Mock a class method using mockery and sinon

grimurd

I'm learning to unit test using the node module mockery with sinon.

Using only mockery and a plain class I'm able to inject a mock successfully. However I would like to inject a sinon stub instead of a plain class but I'm having a lot of troubles with this.

The class I am trying to mock:

function LdapAuth(options) {}

// The function that I want to mock.
LdapAuth.prototype.authenticate = function (username, password, callback) {}

And here is the code I'm currently using in my beforeEach() function:

    beforeEach(function() {
        ldapAuthMock = sinon.stub(LdapAuth.prototype, "authenticate", function(username, password, callback) {});
        mockery.registerMock('ldapauth-fork', ldapAuthMock);
        mockery.enable();
    });

    afterEach(function () {
        ldapAuthMock.restore();
        mockery.disable();
    });

I've tried to mock/stub the LdapAuth class in various ways without success and the code above is just the latest version that doesn't work.

So I just want to know how to mock this successfully using sinon and mockery.

Sveinn Fannar

These node mocking libraries can be quite cumbersome because of Node's module cache, Javascript's dynamic nature and it's prototypical inheritance.

Fortunately Sinon also takes care of modifying the object you are trying to mock as well as providing a nice API to construct spys, subs and mocks.

Here is a small example of how I would stub the authenticate method:

// ldap-auth.js

function LdapAuth(options) {
}

LdapAuth.prototype.authenticate = function (username, password, callback) {
  callback(null, 'original');
}

module.exports = LdapAuth;
// test.js

var sinon = require('sinon');
var assert = require('assert');
var LdapAuth = require('./ldap-auth');

describe('LdapAuth#authenticate(..)', function () {
  beforeEach(function() {
    this.authenticateStub = sinon
                              // Replace the authenticate function
                              .stub(LdapAuth.prototype, 'authenticate')
                              // Make it invoke the callback with (null, 'stub')
                              .yields(null, 'stub');
  });

  it('should invoke the stubbed function', function (done) {
    var ldap = new LdapAuth();
    ldap.authenticate('user', 'pass', function (error, value) {
      assert.ifError(error);
      // Make sure the "returned" value is from our stub function
      assert.equal(value, 'stub');
      // Call done because we are testing an asynchronous function
      done();
    });
    // You can also use some of Sinon's functions to verify that the stub was in fact invoked
    assert(this.authenticateStub.calledWith('user', 'pass'));
  });

  afterEach(function () {
    this.authenticateStub.restore();
  });
});

I hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to mockery mock internal method

From Dev

Mockery mock method inside closure

From Dev

How to mock only one method using Sinon?

From Dev

Mock fs module using mockery

From Dev

Mockery & PHPUnit: method does not exist on this mock object

From Dev

method does not exist on this mock object - Laravel , Mockery

From Dev

Mockery & PHPUnit: method does not exist on this mock object

From Dev

Mockery mock and spy called 0 times on custom class in controller

From Dev

Python: using Mock to override class method

From Dev

Stubbing an ES6 class method using Mocha and Sinon in NodeJS

From Dev

Stubbing an ES6 class method using Mocha and Sinon in NodeJS

From Dev

How to stub a class method inside a function under test using sinon?

From Dev

Using Mockito to mock a class method inside another class

From Dev

Mock a class method

From Dev

Stubbing a get method using Sinon

From Java

How to mock just one static method in a class using Mockito?

From Dev

Unable to mock class method on CLLocationManager using OCMock with iOS7

From Dev

Unable to mock class method on CLLocationManager using OCMock with iOS7

From Dev

Laravel mock with Mockery Eloquent models

From Dev

Laravel Mockery: mock with return logic

From Dev

Why does this Sinon mock have a mocked method that is not a function?

From Dev

Class 'Mockery' not found

From Dev

Class 'Mockery' not found

From Java

Stubbing a class method with Sinon.js

From Dev

Mock a DAO class and a method within it

From Dev

Checking Mockery arguments using Mockery::contains( )

From Dev

Mock method implementation using Google Mock

From Dev

How to mock private method of another Class other than the Class under test using PowerMock?

From Dev

Stub a method that does a callback using Sinon

Related Related

  1. 1

    How to mockery mock internal method

  2. 2

    Mockery mock method inside closure

  3. 3

    How to mock only one method using Sinon?

  4. 4

    Mock fs module using mockery

  5. 5

    Mockery & PHPUnit: method does not exist on this mock object

  6. 6

    method does not exist on this mock object - Laravel , Mockery

  7. 7

    Mockery & PHPUnit: method does not exist on this mock object

  8. 8

    Mockery mock and spy called 0 times on custom class in controller

  9. 9

    Python: using Mock to override class method

  10. 10

    Stubbing an ES6 class method using Mocha and Sinon in NodeJS

  11. 11

    Stubbing an ES6 class method using Mocha and Sinon in NodeJS

  12. 12

    How to stub a class method inside a function under test using sinon?

  13. 13

    Using Mockito to mock a class method inside another class

  14. 14

    Mock a class method

  15. 15

    Stubbing a get method using Sinon

  16. 16

    How to mock just one static method in a class using Mockito?

  17. 17

    Unable to mock class method on CLLocationManager using OCMock with iOS7

  18. 18

    Unable to mock class method on CLLocationManager using OCMock with iOS7

  19. 19

    Laravel mock with Mockery Eloquent models

  20. 20

    Laravel Mockery: mock with return logic

  21. 21

    Why does this Sinon mock have a mocked method that is not a function?

  22. 22

    Class 'Mockery' not found

  23. 23

    Class 'Mockery' not found

  24. 24

    Stubbing a class method with Sinon.js

  25. 25

    Mock a DAO class and a method within it

  26. 26

    Checking Mockery arguments using Mockery::contains( )

  27. 27

    Mock method implementation using Google Mock

  28. 28

    How to mock private method of another Class other than the Class under test using PowerMock?

  29. 29

    Stub a method that does a callback using Sinon

HotTag

Archive