【发布时间】:2017-03-25 07:54:10
【问题描述】:
我目前是Sinon、Mocha、Supertest 的新手,正在编写测试。在我当前的场景中,我有验证我的“OTP”的身份验证库,并在验证后继续在回调函数中执行操作。
我无法模拟回调以返回 null 并继续测试其余代码。以下是我的代码 sn-p:
Controller.js
var authy = require('authy')(sails.config.authy.token);
authy.verify(req.param('aid'), req.param('oid'), function(err, response) {
console.log(err);
if (err) {
return res.badRequest('verification failed.');
}
....
我的测试是:
var authy = require('authy')('token');
describe('Controller', function() {
before(function() {
var authyStub = sinon.stub(authy, 'verify');
authyStub.callsArgWith(2, null, true);
});
it('creates a test user', function(done) {
// This function will create a user again and again.
this.timeout(15000);
api.post('my_endpoint')
.send({
aid: 1,
oid: 1
})
.expect(201, done);
});
});
我本质上想在回调中调用 authy verify get a null 作为“err”,这样我就可以测试其余的代码。
任何帮助将不胜感激。 谢谢
【问题讨论】:
-
@philnash,你能告诉我如何模拟 authy 验证
标签: javascript twilio sinon supertest authy