gpt4 book ai didi

Javascript 测试 : Passing around Jasmine 2. 0's "完成“功能 - 不是我所期望的

转载 作者:行者123 更新时间:2023-11-28 21:29:40 25 4
gpt4 key购买 nike

我对异步测试比较陌生,以下行为让我感到惊讶。我错过了什么?我很感激有人指出我正确的方向

如果这有效 [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()

最佳答案

async 方法将永远不会在第二个示例中被调用,因为它被监视了。你可以改变

spyOn o, "async"

spyOn(o, "async").and.callThrough()

所以该方法被侦测并且所有调用都被委托(delegate)给 async 方法。

关于Javascript 测试 : Passing around Jasmine 2. 0's "完成“功能 - 不是我所期望的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com