为什么当我运行这个测试这个兴农间谍不会被调用? [英] Why is this sinon spy not being called when I run this test?

查看:113
本文介绍了为什么当我运行这个测试这个兴农间谍不会被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个骨干型号:

class DateTimeSelector extends Backbone.Model

  initialize: ->
    @bind 'change:date', @updateDatetime
    @bind 'change:time', @updateDatetime

  updateDatetime: =>
    # do some stuff with the sate and time

和我有使用茉莉花并的 sinon.js

describe "DateTimeSelector", ->
  beforeEach ->
    @datetime = new DateTimeSelector()

    describe "updateDatetime", ->
      beforeEach ->
        @updateSpy = sinon.spy(@datetime, 'updateDatetime')

      afterEach ->
        @datetime.updateDatetime.restore()

      # passes
      it "should be called when we call it", ->
        @datetime.updateDatetime()
        expect(@updateSpy).toHaveBeenCalledOnce()

      # fails
      it "should be called when we trigger it", ->
        @datetime.trigger 'change:date'
        expect(@updateSpy).toHaveBeenCalled()

      # fails
      it "should be called when we set the date", ->
        @datetime.set { date: new Date() }
        expect(@updateSpy).toHaveBeenCalled()

看来,当我使用它在浏览器,但我似乎无法得到测试传递工作。任何人都可以告诉我吗?

It seems to work when I use it in the browser but I can't seem to get the tests to pass. Can anyone enlighten me?

推荐答案

duckyfuzz,你是因为当你正在创建的间谍(实际上包装了原有的功能,并创建了一个间接层插入其跟踪的服务遇到此问题方法调用)的事件的结合已经发生。这意味着,即使间谍包裹原有功能的事件绑定引用原有的功能,而不是被包装的间谍。因此,当你测试,原有的功能得到对事件触发执行,但间谍跟踪的上一级,并且不执行。

duckyfuzz, you are experiencing this problem because when you are creating the spy (which actually wraps the original function and creates a level of indirection to insert its services of tracking method invocation) the binding of the events has already taken place. Which means that even though the spy wrapped the original function the event binding references the original function and not the wrapped spy. Hence, when you test, the original function gets executed on the event trigger but the spy tracking is one level above and is not executed.

要确保事件绑定实际上是指向你必须先创建间谍创建模型对象(也是如此,如果你正在测试的意见)包装的间谍功能。要做到这一点的原型创建间谍之类的方法:

To make sure that the event binding is actually pointing to the wrapped spy function you have to create the spy before create the model object (same goes if you are testing views). To do that create the spy on the prototype."method" of the class:

beforeEach - > @datetime =新DateTimeSelector()创建间谍之前部分: @updateSpy = sinon.spy( DateTimeSelector。原型的updateDatetime')

in the beforeEach -> section before @datetime = new DateTimeSelector() create the spy: @updateSpy = sinon.spy(DateTimeSelector.prototype, 'updateDatetime')

请务必更改您的 afterEach - > 部分,你回原型恢复正常,像这样: @ updateSpy.restore()

be sure to change your afterEach -> section where you return the prototype back to normal, like so: @updateSpy.restore()

这应该是你的code:

this should be your code:

describe "DateTimeSelector", ->
  beforeEach ->
    @updateSpy = sinon.spy(DateTimeSelector.prototype, 'updateDatetime')
    @datetime = new DateTimeSelector()

  afterEach ->
    @updateSpy.restore()

  # passes
  it "should be called when we call it", ->
    @datetime.updateDatetime()
    expect(@updateSpy).toHaveBeenCalledOnce()

  # should pass now
  it "should be called when we trigger it", ->
    @datetime.trigger 'change:date'
    expect(@updateSpy).toHaveBeenCalled()

  # should pass now
  it "should be called when we set the date", ->
    @datetime.set { date: new Date() }
    expect(@updateSpy).toHaveBeenCalled() 

顺便说一句,如果你使用的是茉莉,sinon.js插件那么你的语法是罚款

BTW, if you are using jasmin-sinon.js plugin then your syntax is fine

这篇关于为什么当我运行这个测试这个兴农间谍不会被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆