我目前對Sinon,Mocha,Supertest以及編寫測試的過程都很陌生。在我目前的情況下,我有驗證庫驗證我的「OTP」,並驗證它繼續執行回調函數內的操作。Sinon - 如何驗證庫(Authy -Twilio)
我無法模擬回調返回null並繼續測試代碼的其餘部分。以下是我的代碼片段:
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驗證得到一個空的回調「犯錯」,這樣我就可以測試代碼的其餘部分。
任何幫助將不勝感激。 謝謝
@philnash,可以請你讓我怎麼嘲笑authy驗證 –