茉莉花间谍未定义

Joh*_*ohn 3 javascript coffeescript jasmine jasmine-jquery

当我运行我的茉莉花规格时,我收到以下错误:

Error: Expected a spy, but got undefined.
Run Code Online (Sandbox Code Playgroud)

我的coffeescript代码:

  describe "setupForm", ->
    beforeEach ->
      spyOn(Subscription.prototype, 'runSimulation')

    it "calls subscription.runSimulation when form is submitted with number", ->
      Subscription.prototype.runSimulation()
      expect(Subscription.prototype.runSimulation()).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)

我已经将我的错误代码简化为上面的调试,但是我无法弄清楚为什么当我明确地将它称为我的测试时它是从未调用过的.我正在其他地方测试该方法,所以我认为错误必须与我如何使用Jasmine Spy有关.谢谢.

Chr*_*erg 14

就拿()离结束Subscription.prototype.runSimulation():

  expect(Subscription.prototype.runSimulation).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)

  • 如果你传递`Subscription.prototype.runSimulation()`,你实际上是传递函数的*返回值*,而不是函数本身.返回值(我假设)是未定义的,这就是您收到该错误的原因. (4认同)