【发布时间】:2018-09-09 22:50:09
【问题描述】:
我使用 mocha、sinon 和 chai 运行一组测试。这是我正在测试的方法
fn.method().then(function(response) {
console.log(response)
test()
})
我已经像这样删除了method
test = sinon.spy()
fn = { method: sinon.stub().resolves("hello") }
在我的测试中我有
expect(fn.method()).to.have.been.called
console.log("goodbye")
expect(test).to.have.been.called
我希望测试通过并按顺序打印“hello”和“goodbye”,但我看到expect(test).to.have.been.called 失败,而且我看到“hello”在“goodbye”之后打印。
在 promise 解决后测试 test 是否被调用的正确方法是什么?
【问题讨论】:
-
您应该查看 async 和 await :[参考链接][1] [1]: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript unit-testing promise sinon stub