【发布时间】:2017-07-29 08:47:32
【问题描述】:
describe("/test" , ()=> {
// separate class 2
class2 = {
// function that i wanna stub
hi: function () {
return "hi";
}
}
// separate class 1
class1 = {
// function that i have stubbed and tested
method1: function() {
return new Promise((resolve, reject) => {
resolve(num);
})
}
}
// method that i will execute
var parent= function (){
class1.method1().then(()=>{
class2.hi();
})
}
// the test
it("should stub hi method",()=>{
var hiTest = sinon.stub(class2, 'hi').resolves(5);
var method1Test = sinon.stub(class1 , 'method1').resolves(5);
// this start the execution of the promise with then call
parent();
// this works fine and test pass
expect(method1Test.calledOnce.should.be.true);
// this doesn't work although i executed the function
expect(hiTest.calledOnce.should.be.true);
})
})
我想做的是正确测试 hi 方法 ..因为当我测试该方法是否执行一次时
虽然我在 then 调用 promise 时执行了它,但它并没有显示出来,并且它使 calledOnce 测试失败
【问题讨论】:
标签: unit-testing promise mocha.js sinon chai