【发布时间】:2016-04-23 01:07:23
【问题描述】:
假设我有一个这样导出的模块:
module.exports = mymodule;
然后在我的测试文件中,我需要模块并存根它。
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();
}));
});
});
这行得通,测试通过了!但是如果我需要导出多个对象,一切都会崩溃。见下文:
module.exports = {
api: getSports,
other: other
};
然后我尝试调整我的测试代码:
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();
}));
});
});
在这种情况下,我的测试失败了。如何更改我的存根代码才能工作?谢谢!
【问题讨论】:
标签: javascript node.js mocha.js sinon