茉莉花未能在async.waterfall块内调用函数

问题描述:

我有一个node.js应用程序,使用async utilities来破坏嵌套的回调。茉莉花未能在async.waterfall块内调用函数

而且我试图在我的茉莉花规格中侦测由async.waterfall包围的函数,但总是失败。

下面的代码可以重现该错误:

async = require 'async' 

app = hi: -> 

fn = -> 
    # app.hi() # works 
    async.waterfall [ 
    (cb) -> 
     app.hi() # doesn't work 
     cb null 
    ], (err) -> 

describe 'jasmine', -> 
    beforeEach -> 
     spyOn app, 'hi' 
    it 'test async.waterfall', -> 
    spyOn app, 'hi' 
    fn() 
    expect(app.hi).toHaveBeenCalled() 

的失败消息:

Failures: 

    1) jasmine test async.waterfall 
    Message: 
    Expected spy hi to have been called. 
    Stacktrace: 
    Error: Expected spy hi to have been called. 
    at new jasmine.ExpectationResult (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:114:32) 
    at null.toHaveBeenCalled (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:1235:29) 
    at null.<anonymous> (/Volumes/ws/prj/litb/crm/tests/job/indexSpecs.coffee:51:29) 
    at jasmine.Block.execute (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:1064:17) 
    at jasmine.Queue.next_ (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2096:31) 
    at jasmine.Queue.start (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2049:8) 
    at jasmine.Spec.execute (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2376:14) 
    at jasmine.Queue.next_ (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2096:31) 
    at jasmine.Queue.start (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2049:8) 
    at jasmine.Suite.execute (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2521:14) 
    at jasmine.Queue.next_ (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2096:31) 
    at onComplete (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2092:18) 
    at jasmine.Suite.finish (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2478:5) 
    at null.onComplete (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2522:10) 
    at jasmine.Queue.next_ (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2106:14) 
    at onComplete (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2092:18) 
    at jasmine.Spec.finish (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2350:5) 
    at null.onComplete (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2377:10) 
    at jasmine.Queue.next_ (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2106:14) 
    at null._onTimeout (/Volumes/ws/prj/litb/crm/node_modules/jasmine-node/lib/jasmine-node/jasmine-1.3.1.js:2086:18) 
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

但是它传递,如果函数调用(这应该是一个间谍)是瀑布块外。

我想知道我的代码有问题吗?或者它不被茉莉花或异步支持?

由于waterfall是异步的,规范正在完成而没有意识到它应该等待瀑布发生。你可以使用茉莉花的异步支持来解决这个问题,虽然茉莉花的异步的东西被认为不是那么好。

it 'test async.waterfall', -> 
    spy = spyOn app, 'hi' 
    runs -> 
    fn() 

    waitsFor -> 
    spy.callCount > 0 

    runs -> 
    # kind of redundant at this point, 
    # the waitsFor already asserted this 
    expect(app.hi).toHaveBeenCalled() 

另一种方法是使setTimeout和/或'的setInterval不会在您的测试环境异步。这也有缺点:

beforeEach -> 
    # I'm sure jasmine's spys can handle this too 
    # I use sinon myself, not as familiar with jasmine's spies 
    @realSetTimeout = window.setTimeout 
    window.setTimeout = (fn, delay) -> fn() 

afterEach -> 
    window.setTimeout = @realSetTimeout 

it 'test async.waterfall', -> 
    # as you have it now 
+0

谢谢,它的工作原理。实际上,在发布这个问题之前,我确实尝试过运行/等待,但是我把间谍放在'beforeEach'部分,并且它不起作用,那么在'beforeEach'和spec块中定义间谍有什么区别? – xinthink 2013-04-28 05:13:21