如何测试expressJS控制器
试图对控制器进行单元测试,但是当我这样做时,出现以下错误。
我很乐意以不同的方式测试控制器。
错误:
TypeError:预期的sinon对象
const test = require('sinon-test');
describe('index (get all)', function() {
beforeEach(function() {
res = {
json: sinon.spy(),
status: sinon.stub().returns({ end: sinon.spy() })
};
expectedResult = [{}, {}, {}];
});
it(
'should return array of vehicles or empty array',
test(() => {
this.stub(Vehicle, 'find').yields(null, expectedResult);
Controller.index(req, res);
sinon.assert.calledWith(Vehicle.find, {});
sinon.assert.calledWith(res.json, sinon.match.array);
})
);
});
回答如下:尝试使用mocha测试框架和chai断言库。https://youtu.be/MLTRHc5dk6s这将给您一个很好的介绍。
也尝试茉莉花
如何测试expressJS控制器
试图对控制器进行单元测试,但是当我这样做时,出现以下错误。
我很乐意以不同的方式测试控制器。
错误:
TypeError:预期的sinon对象
const test = require('sinon-test');
describe('index (get all)', function() {
beforeEach(function() {
res = {
json: sinon.spy(),
status: sinon.stub().returns({ end: sinon.spy() })
};
expectedResult = [{}, {}, {}];
});
it(
'should return array of vehicles or empty array',
test(() => {
this.stub(Vehicle, 'find').yields(null, expectedResult);
Controller.index(req, res);
sinon.assert.calledWith(Vehicle.find, {});
sinon.assert.calledWith(res.json, sinon.match.array);
})
);
});
回答如下:尝试使用mocha测试框架和chai断言库。https://youtu.be/MLTRHc5dk6s这将给您一个很好的介绍。
也尝试茉莉花