私は現在、Sinon、Mocha、Supertest を初めて使用し、テストを作成中です。私の現在のシナリオでは、「OTP」を検証する認証ライブラリがあり、検証後、コールバック関数内で操作を実行します。
コールバックをモックして 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 verify get null を「err」として呼び出したいので、残りのコードをテストできます。
どんな助けでも大歓迎です。ありがとう