【发布时间】:2014-09-22 10:44:19
【问题描述】:
我是在 node.js 中进行测试的新手,我想在如下所示的过程中模拟特定函数调用的返回。
doSomething(function(err, res){
callAnotherOne(res, function(err, result){
getDataFromDB(result, function(err, docs){
//some logic over the docs here
})
})
})
我要模拟的函数是 getDataFromDB(),特别是它返回的文档(使用 MongoDB)。 我怎么能用摩卡做这样的事情?
从中间的逻辑中剥离出来的部分代码如下:
filterTweets(item, input, function(err, item) {
//Some filtering and logging here
db.getTwitterReplies(item, function(err, result) {
if(err) {
return callback('Failed to retrieve tweet replies');
}
//Do some work here on the item using the result (tweet replies)
/***** Here I want to test that the result is the expected ****/
db.storeTweets(item function (err, result){
//error checks, logging
callback();
});
});
});
根据 twitter 回复的数量(函数调用“getTwitterReplies”),我将相应地修改我的对象(不包括该代码)。我想看看是否基于不同的回复结果,我的对象是否按预期构造。
附言经过一番搜索,我还检查了 sinon.js,我设法模拟了回调的返回(通过在我的项目之外编写一些测试代码),但没有模拟嵌套函数调用的回调的返回。
【问题讨论】:
-
如果没有看到这一切如何联系在一起的示例,我真的想不出一个解决方案。
-
我通过添加我想模拟的代码来编辑我的答案。
-
require() 的作用域可以提供一种在 sinon.js 测试中轻松进行依赖注入的方法,而无需引入大量额外的函数参数。我将在这些方面提供更广泛的答案。
标签: node.js unit-testing mocha.js sinon