Reputation: 1798
Let's say I have a module that exports like this:
module.exports = mymodule;
Then in my test file, I require the module and stub it.
var mymodule = require('./mymodule');
describe('Job gets sports data from API', function(){
context('When there is a GET request', function(){
it('will call callback after getting response', sinon.test(function(done){
var getRequest = sinon.stub(mymodule, 'getSports');
getRequest.yields();
var callback = sinon.spy();
mymodule.getSports(callback);
sinon.assert.calledOnce(callback);
done();
}));
});
});
That works and the test passes! But everything breaks down if I need to export more than one object. See below:
module.exports = {
api: getSports,
other: other
};
Then I try to adjust my test code:
var mymodule = require('./mymodule');
describe('Job gets sports data from API', function(){
context('When there is a GET request', function(){
it('will call callback after getting response', sinon.test(function(done){
var getRequest = sinon.stub(mymodule.api, 'getSports');
getRequest.yields();
var callback = sinon.spy();
mymodule.api.getSports(callback);
sinon.assert.calledOnce(callback);
done();
}));
});
});
In this case, my test craps out. How can I change my stub code to work? Thanks!
Upvotes: 1
Views: 832
Reputation: 1688
Based on this
module.exports = {
api: getSports,
other: other
};
it looks like mymodule.api
doesn't itself have a getSports
method. Rather, mymodyle.api
is a reference to a getSports
function insider your module.
Instead of stubbing getSports
you would need to stub api
:
var getRequest = sinon.stub(mymodule, 'api');
However, given how you're trying to stub getSports
, you might instead want to update how you are exporting the function instead of how you are stubbing it.
Upvotes: 1