我对异步测试比较新,以下行为让我感到惊讶。我错过了什么?我很感激有人指出我正确的方向
如果这有效[Coffeescript]:
# Works
describe "Asynchronous specs", ->
o = {}
beforeEach (done) ->
o.async = ->
console.log "Finished"
spyOn o, "async"
setTimeout (->
o.async()
done()
), 1500
it "async executed", ->
expect o.async
.toHaveBeenCalled()
为什么不将done()
函数作为参数传递也有效?
# doesnt work: Test times out
describe "Asynchronous specs", ->
o = {}
beforeEach (done) ->
o.async = (passedDone)->
passedDone()
console.log "Finished"
spyOn o, "async"
setTimeout (=>
o.async done
), 1500
it "async executed", ->
expect o.async
.toHaveBeenCalled()
答案 0 :(得分:1)
在第二个示例中永远不会调用async
方法,因为它被监视。你可以改变
spyOn o, "async"
到
spyOn(o, "async").and.callThrough()
所以该方法是间谍的,并且所有调用都被委托给async
方法。