为什么西农认不出我的存根
Why doesn't sinon recognize my stub?
假设我有一个像这样导出的模块:
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();
}));
});
});
在这种情况下,我的测试失败了。 如何更改我的存根代码以使其正常工作? 谢谢!
基于此
module.exports = {
api: getSports,
other: other
};
看起来mymodule.api
本身没有getSports
方法。相反,mymodyle.api
是对模块内部getSports
函数的引用。
而不是存根getSports
您需要存根api
:
var getRequest = sinon.stub(mymodule, 'api');
但是,考虑到您尝试存根getSports
的方式,您可能希望更新导出函数的方式,而不是存根方式。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 我的单元测试选项是什么
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 我的职位回报太快了,如何做出承诺
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何识别我的网站中的慢速设备
- 如何将JSON数据导入我的ejs模板
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 知道为什么我的旋转木马不会自动更改图片吗
- 我的模板未被解析
- 无法将数据从firebase获取到我的html页面
- 角度图表;t显示在我的页面中
- 我的AngularJS表达式没有'不起作用
- 将电视直播频道从网站嵌入我的网站
- /undefined在我的404错误日志中多次出现
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我的javascript for循环不起作用
- 为什么西农认不出我的存根