我的单元测试之一失败了。我认为这是因为存根返回错误的响应类型。也许我只是在测试错误的东西。这是我的方法
let categories = Categories.create();
let totalVideos = await Video.getTotalVideos();
await categories.load();
const data = categories.getData();
if(data.length && totalVideos > 0) {
let videoArray = [];
for (let i = 0, categoryLength = data.length; i < categoryLength; i++) {
let objId = data[i]._id;
let results = await Search.findByCategoryId(objId.valueOf());
...
}
return utils.sendResponse(res, 200, utils.statusCodes.Success, {stats: videoArray});
这是我的测试:
describe('GET /v1/search/videos/categories', () => {
let stub, stubVideo, stubData;
beforeEach(() => {
req = httpMocks.createRequest({
url: '/v1/search/videos/categories',
method: 'GET'
});
stub = sinon.stub(Categories.prototype, 'load');
stubData = sinon.stub(Categories.prototype, 'getData');
stubVideo = sinon.stub(Video, 'getTotalVideos');
});
afterEach(() => {
stub.restore();
stubData.restore();
stubVideo.restore();
});
it('should return a 200 if success', async () => {
stub.resolves([{"_id": new ObjectId()}]);
stubData.resolves([{"_id": new ObjectId()}, {"_id": new ObjectId()}]);
stubVideo.resolves(10);
expect(stubVideo).to.be.above(0);
expect(stubData).to.have.length.above(0);
await searchController.getAllVideosByCategory(req, res);
expect(res.statusCode).to.equal(200);
});
});
运行测试时,对getTotalVideos的断言响应为“ AssertionError:预期getTotalVideos为数字或日期”。获取类别数组的断言响应为“ AssertionError:预期[Function:proxy]的长度大于0但为0”。
要使整个测试正常进行,
videoCount > 0 && categories.length > 0
我如何获取存根以返回正确的响应类型和值以使其正常工作?
我只添加了断言,因为await searchController.getAllVideosByCategory(req, res)
一直返回404,这意味着出于某些原因未满足videoCount && category.length条件,这些断言告诉我,没有返回必要的值来满足这些条件。因此,如果还有其他方法可以返回这些值,使该方法返回200,请告诉我。
答案 0 :(得分:0)
expect(stubVideo).to.be.above(0)
断言stubVideo
是正数,而它是一个返回正数的函数。测试存根没有意义,它们是由您设置的。但是可能有断言称它们为:
expect(stubVideo).to.have.been.calledOnce;
expect(stubVideo).to.have.been.calledWith();
该测试应在调用utils.sendResponse
之前进行监视,并断言已使用相应的参数调用了该测试。
答案 1 :(得分:0)
我一直在试图了解您想要实现的目标。
我发现代码中有一些需要改进的地方:
await
源文件中添加categories.getData()
。sandbox
简化存根代码resolves
添加Search.findByCategoryId
to.be.above
等,因为您已经将其存入了预期的值。 我的代码
describe('GET /v1/search/videos/categories', () => {
let categoryCreateStub;
let categoryLoadStub;
let categoryGetDataStub;
let getTotalVideoStub;
beforeEach(() => {
req = httpMocks.createRequest({
url: '/v1/search/videos/categories',
method: 'GET'
});
sandbox = sinon.sandbox.create(); // use sandbox for multiple stubs
categoryLoadStub = sandbox.stub(Categories.prototype, 'load');
categoryGetDataStub = sandbox.stub(Categories.prototype, 'getData');
getTotalVideoStub = sandbox.stub(Video, 'getTotalVideos');
searchFindByCategoryIdStub = sandbox.stub(Search, 'findByCategoryId');
});
afterEach(() => {
sandbox.restore(); // just need to call this to restore
});
it('should return a 200 if success', async () => {
categoryLoadStub.resolves([{
"_id": new ObjectId()
}]);
categoryGetDataStub.resolves([{
"_id": new ObjectId()
}, {
"_id": new ObjectId()
}]);
getTotalVideoStub.resolves(10);
searchFindByCategoryIdStub.resolves(); // resolve this promise too!
await searchController.getAllVideosByCategory(req, res);
expect(res.statusCode).to.equal(200);
});
});
希望有帮助。