1

我有一个 node.js 应用程序,使用异步实用程序来打破嵌套回调。

而且我试图在我的茉莉花规格中监视由 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)

但如果函数调用(应该是间谍)在瀑布块之外,它就会通过。

我想知道我的代码是否有问题?还是茉莉花或异步不支持?

4

1 回答 1

2

由于waterfall是异步的,规范正在完成而没有意识到它应该等待瀑布发生。您可以使用 jasmine 的异步支持来解决这个问题,尽管 jasmine 的异步功能被认为不是那么好。

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
于 2013-04-28T04:00:55.170 回答