1

我正在尝试编写一个茉莉花间谍来侦听骨干模型事件触发器。间谍在某种程度上是有效的:它监视每一个model.trigger电话。有没有办法只能让它寻找特定的电话?

例如,如果我想监视@trigger 'error',但不想监视,@trigger 'change'我可以这样做吗?在我下面的模型中,该@set调用还触发了一个 Backbonechange事件,该事件触发了我的触发器间谍并导致测试失败。

骨干模型定义:

class @AdminDatasetModel extends Backbone.Model
  toggleEnabled: ->
    @set 'enabled', !@get('enabled')
    $.ajax
      url: "#{@urlRoot}/#{@get('id')}/enable"
      type: 'POST'
      data: 
        enabled: @get 'enabled'
      success: (data) =>
        PDLS.vent.trigger 'admin:itemToggled', @
      error: (jqXHR) =>
        @trigger 'error', 'There was a problem updating your dataset!'

茉莉花规格定义:

describe 'An admin dataset model', ->
    MOCK_GET_DATA =
        id: 1
        name: 'someName'
        enabled: true
        permissions:
            userId: 2

    MOCK_SUCCESS_DATA =
        succes: true

    MOCK_ERROR_DATA =
        error: true

    dataset = null

    beforeEach ->
        PDLS = new PDLSApp
        # Always return true for ajax requests, unless we specify otherwise
        spyOn($, 'ajax').andCallFake (params) ->
            params.success = MOCK_SUCCESS_DATA

        @triggerSpy = spyOn AdminDatasetModel.prototype, 'trigger'

        it 'should bubble up an error when appropriate', ->
            $.ajax.isSpy = false
            spyOn($, 'ajax').andCallFake (params) ->
                params.error MOCK_ERROR_DATA

            toggleSpy = spyOn AdminDatasetModel.prototype, 'toggleEnabled'
            dataset = new AdminDatasetModel(MOCK_GET_DATA)
            dataset.toggleEnabled()

            expect(toggleSpy).toHaveBeenCalled()
            expect(@triggerSpy).toHaveBeenCalledWith 'error', 'There was a problem updating your dataset!'
4

1 回答 1

0

我通常更喜欢做类似的事情:

listener = jasmine.createSpy 'listener'
model = new AdminDatasetModel(MOCK_GET_DATA)
model.on 'error', listener
model.toggleEnabled()
expect(listener).toHaveBeenCalledWith 'There was a problem updating your dataset!'

而不是直接监视trigger

于 2014-01-25T20:14:09.740 回答