我正在尝试编写一个茉莉花间谍来侦听骨干模型事件触发器。间谍在某种程度上是有效的:它监视每一个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!'