单元测试猫鼬对 sinon 的承诺
Unit test mongoose promises with sinon
我正在尝试对使用承诺的猫鼬对象进行单元测试。我已经在下面编写了测试,它可以工作,但还不完整。我不知道如何测试是否调用了 'then' 或 'catch' 方法。
我如何使用间谍来检查 'then' 方法在我 resolve promise 时被调用?
测试方法
export function create(req, res) {
User
.createAsync(req.body)
.then(handleCreate(res, req.originalUrl))
.catch(handleError(res));
}
单元测试
it('should do something', () => {
const req = {
body: 45,
};
const res = {};
const mockRole = sandbox.mock(Role).expects('createAsync').once().withArgs(45)
.returns(Promise.resolve());
controller.create(req, res);
});
更新我使用的解决方案(2016 年 5 月 6 日)
感谢@ReedD 在正确的方向上帮助我
虽然这样 "works",但我觉得我在测试 promises 的功能,而不是我的代码。
it('should call create with args and resolve the promise', () => {
const createSpy = sinon.spy();
const errorSpy = sinon.spy();
sandbox.stub(responses, 'responseForCreate').returns(createSpy);
sandbox.stub(responses, 'handleError').returns(errorSpy);
sandbox.mock(Role).expects('createAsync').once().withArgs(45)
.returns(Promise.resolve());
return controller.create(req, res).then(() => {
expect(createSpy.calledOnce).to.be.equal(true);
expect(errorSpy.calledOnce).to.be.equal(false);
});
});
您可以将 handleCreate
和 handleError
添加到 module.exports
,然后制作它们的存根或间谍。以下是我认为您正在尝试做的示例。我还假设您使用的是 sinon/chai.
http://ricostacruz.com/cheatsheets/sinon-chai.html
// controller.js
module.exports = {
handleCreate: function () {
// ..code
},
handleError: function () {
// ..code
},
create: function (req, res) {
User
.createAsync(req.body)
.then(this.handleCreate(res, req.originalUrl))
.catch(this.handleError(res));
}
};
// test/test.js
var controller = require('../controller');
it('should do something', function (done) {
var handleCreate = sandbox.spy(controller, 'handleCreate');
var handleError = sandbox.spy(controller, 'handleError');
var mockRole = sandbox
.mock(Role)
.expects('createAsync')
.once().withArgs(45)
.returns(Promise.resolve());
var req = {
body: 45,
};
var res = {
send: function () {
expect(handleCreate).to.be.calledOnce;
expect(handleError).to.not.be.called;
done();
}
};
controller.create(req, res);
});
我正在尝试对使用承诺的猫鼬对象进行单元测试。我已经在下面编写了测试,它可以工作,但还不完整。我不知道如何测试是否调用了 'then' 或 'catch' 方法。
我如何使用间谍来检查 'then' 方法在我 resolve promise 时被调用?
测试方法
export function create(req, res) {
User
.createAsync(req.body)
.then(handleCreate(res, req.originalUrl))
.catch(handleError(res));
}
单元测试
it('should do something', () => {
const req = {
body: 45,
};
const res = {};
const mockRole = sandbox.mock(Role).expects('createAsync').once().withArgs(45)
.returns(Promise.resolve());
controller.create(req, res);
});
更新我使用的解决方案(2016 年 5 月 6 日)
感谢@ReedD 在正确的方向上帮助我
虽然这样 "works",但我觉得我在测试 promises 的功能,而不是我的代码。
it('should call create with args and resolve the promise', () => {
const createSpy = sinon.spy();
const errorSpy = sinon.spy();
sandbox.stub(responses, 'responseForCreate').returns(createSpy);
sandbox.stub(responses, 'handleError').returns(errorSpy);
sandbox.mock(Role).expects('createAsync').once().withArgs(45)
.returns(Promise.resolve());
return controller.create(req, res).then(() => {
expect(createSpy.calledOnce).to.be.equal(true);
expect(errorSpy.calledOnce).to.be.equal(false);
});
});
您可以将 handleCreate
和 handleError
添加到 module.exports
,然后制作它们的存根或间谍。以下是我认为您正在尝试做的示例。我还假设您使用的是 sinon/chai.
http://ricostacruz.com/cheatsheets/sinon-chai.html
// controller.js
module.exports = {
handleCreate: function () {
// ..code
},
handleError: function () {
// ..code
},
create: function (req, res) {
User
.createAsync(req.body)
.then(this.handleCreate(res, req.originalUrl))
.catch(this.handleError(res));
}
};
// test/test.js
var controller = require('../controller');
it('should do something', function (done) {
var handleCreate = sandbox.spy(controller, 'handleCreate');
var handleError = sandbox.spy(controller, 'handleError');
var mockRole = sandbox
.mock(Role)
.expects('createAsync')
.once().withArgs(45)
.returns(Promise.resolve());
var req = {
body: 45,
};
var res = {
send: function () {
expect(handleCreate).to.be.calledOnce;
expect(handleError).to.not.be.called;
done();
}
};
controller.create(req, res);
});