Javascript 如何在异步测试中为sinon间谍计时
我有异步测试,其中一个间谍在一个测试中启动,下一个测试运行,而间谍仍在第一个测试中包装。这意味着Javascript 如何在异步测试中为sinon间谍计时,javascript,mocha.js,sinon,Javascript,Mocha.js,Sinon,我有异步测试,其中一个间谍在一个测试中启动,下一个测试运行,而间谍仍在第一个测试中包装。这意味着断言(spy.calledOnce)失败。它被呼叫了两次断言(spy.calledTwice)传递但错误;未及时调用restore() spied方法myModule.myMethod在mycontroller.aMethod 我怎样才能解决这个问题 it('test one', function() { sinon.spy(myModule, 'myMethod') myControler.
断言(spy.calledOnce)
失败。它被呼叫了两次<代码>断言(spy.calledTwice)传递但错误;未及时调用restore()
spied方法myModule.myMethod
在mycontroller.aMethod
我怎样才能解决这个问题
it('test one', function() {
sinon.spy(myModule, 'myMethod')
myControler.aMethod().then(res => {
// second test runs before this is called and calls it a second time
assert(myModule.myMethod.calledOnce)
teamController.getAllTeamSlugs.restore()
})
})
it('test two', function() {
const fakeCache = {}
// this runs before the above test can restore the spy
myControler.aMethod().then(res => {
// some asserts
})
})
单独的descripe块不能解决问题。这是由于缺少
return
语句造成的。块现在将等待正确的执行时间。添加返回使工作代码如下:
it('test one', function() {
sinon.spy(myModule, 'myMethod')
return myControler.aMethod().then(res => {
// second test runs before this is called and calls it a second time
assert(myModule.myMethod.calledOnce)
teamController.getAllTeamSlugs.restore()
})
})
it('test two', function() {
const fakeCache = {}
// this runs before the above test can restore the spy
return myControler.aMethod().then(res => {
// some asserts
})
})
这是由于缺少return
语句造成的。块现在将等待正确的执行时间。添加返回使工作代码如下:
it('test one', function() {
sinon.spy(myModule, 'myMethod')
return myControler.aMethod().then(res => {
// second test runs before this is called and calls it a second time
assert(myModule.myMethod.calledOnce)
teamController.getAllTeamSlugs.restore()
})
})
it('test two', function() {
const fakeCache = {}
// this runs before the above test can restore the spy
return myControler.aMethod().then(res => {
// some asserts
})
})