Stubbing / mocking library that returns a function if necessary

So, I'm trying to ridicule or drown out the call to growl . If necessary, a function is returned that will trigger a notification of the lever when called. I can't figure out how to taunt or drown it out in my tests.

This is what I have tried so far:

/* /lib/some_code.js */
var growl = require('growl');
exports.some_func = function() { 
  growl('A message', { title: 'Title' }); 
};

(Note: I use sinon-chai for my statements)

/* /test/some_code.js */
var growl = require('growl')
  , some_code = require('../lib/some_code');

describe('Some code', function() {
  it('sends a growl notification', function(done) {
    var growlStub = sinon.stub(growl);
    some_code.some_func();
    growlStub.should.have.been.called;
    done();
  });
});
+4
source share
1 answer

So, I came up with a solution that seems to work, although I personally find it a bit unclean.

.

// Code under test
exports.growl = require('growl');
exports.some_func = function() {
  exports.growl('message', { title: 'Title' });
};

// Test
var some_code = require('../lib/some_code');   
describe('Some code', function() {
  it('sends a growl notification', function(done) { 
    var growlStub = sinon.stub(some_code, 'growl');
    some_code.some_func();
    growlStub.should.have.been.called;
    done();
  });
});

- , .

+1

Source: https://habr.com/ru/post/1525029/


All Articles