【发布时间】:2016-12-02 18:52:44
【问题描述】:
我的 nodeJs 代码中有两种方法,例如
function method1(id,callback){
var data = method2();
callback(null,data);
}
function method2(){
return xxx;
}
module.exports.method1 = method1;
module.exports.method2 = method2;
为了使用Sinon 和Mocha 测试函数method1,我必须使用stub 方法method2。
为此需要将方法method2调用为
function method1(id,callback){
var data = this.method2();
callback(null,data);
}
测试代码
describe('test method method2', function (id) {
var id = 10;
it('Should xxxx xxxx ',sinon.test(function(done){
var stubmethod2 = this.stub(filex,"method2").returns(data);
filex.method1(id,function(err,response){
done();
})
})
})
使用此测试用例通过,但代码停止工作并出现错误this.method2 is not a function。
有什么办法可以摆脱 this 或 module.exports 这似乎是错误的。
如果我错过任何其他信息,请告诉我..
【问题讨论】:
-
能否提供完整的测试文件代码?
-
你搞定了吗?
-
这不像是代码工作或测试用例工作的权衡
标签: javascript node.js unit-testing mocha.js sinon-chai