Javascript测试:绕过Jasmine 2.0"完成"功能 - 不是我的预期

时间:2014-12-15 23:14:27

标签: javascript testing coffeescript jasmine

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

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

1 个答案:

答案 0 :(得分:1)

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

spyOn o, "async"

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

所以该方法是间谍的,并且所有调用都被委托给async方法。