Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在异步测试中为sinon间谍计时_Javascript_Mocha.js_Sinon - Fatal编程技术网

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
  })
})