Skip to content Skip to sidebar Skip to footer

Sinon - How To Stub Authentication Library (Authy -Twilio)

I am currently new to Sinon, Mocha, Supertest and in the process to writes tests. In my current scenario, i have authentication library which verifies my 'OTP' and after verifying

Solution 1:

The trouble is that you're using different instances of the authy object in your tests and your code. See here authy github repo.

In your code you do

var authy = require('authy')(sails.config.authy.token);

and in your test

var authy = require('authy')('token');

So your stub is generally fine, but it will never work like this because your code does not use your stub.

A way out is to allow for the authy instance in your controller to be injected from the outside. Something like this:

function Controller(authy) {
    // instantiate authy if no argument passed

in your tests you can then do

describe('Controller', function() {
    before(function() {
        var authyStub = sinon.stub(authy, 'verify');
        authyStub.callsArgWith(2, null, true);
        // get a controller instance, however you do it
        // but pass in your stub explicitly
        ctrl = Controller(authyStub);
    });
});

Post a Comment for "Sinon - How To Stub Authentication Library (Authy -Twilio)"