node.js - Jasmine 无法监视 async.waterfall block 中调用的函数

标签 node.js jasmine-node

我有一个node.js应用程序,使用async utilities ,打破嵌套回调。

我试图监视我的 jasmine 规范中 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)

但如果函数调用(应该是 spy )位于 waterfall block 之外,它就会通过。

我想知道我的代码是否有问题?或者 jasmine 或 async 不支持?

最佳答案

由于 waterfall 是异步的,因此规范正在完成,但没有意识到它应该等待 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

关于node.js - Jasmine 无法监视 async.waterfall block 中调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16259314/

相关文章:

node.js - 无法正确获取intaller_nodejs_windows.msi文件: CRC error

javascript - 你如何强制 Node 模块的绝对路径?

node.js - Node-Jasmine 没有按预期失败

node.js - 如何让 jasmine-ts 使用特定种子执行我的规范?

javascript - 谷歌SDK返回403 `User Rate Limit Exceeded`

javascript - Node.js 中循环异步函数内 undefined variable

java - 运行平均种子时 jasmine_node 失败

Jasmine 找不到 Spec 文件

javascript - 如果当前测试失败,如何检查mocha(tdd)的拆解方法?

javascript - 进行多个异步调用而不相互依赖