我编写 Backbone.Collection 来加载 JSON 文件并在 Backbone.View 中使用
这是我的 Backbone.Collection
class Backbone.Config extends Backbone.Collection
initialize: (filename = 'settings') ->
@_load(filename)
# private load setting file
_load: (filename) ->
@fetch
url : "/assets/backbone/config/#{filename}.json"
error: (e,data) ->
if data.status == 404
throw "Exception: Cant find such a file #{filename}.json"
else if data.status == 200
throw "Exception: can't load json file. please check your JSON syntax"
get: ->
@.toJSON()[0]
我在我的 Backbone.View 中使用它
像这样
error: ->
@displayMessage @config.get().form.display_message.error, 'red', 3000
这很好,但是当我用茉莉花为它写测试时,
描述“事件”,->
beforeEach ->
@view.config = new Backbone.Config()
@successSpy = sinon.spy @view, 'success'
@errorSpy = sinon.spy @view, 'error'
@displayMessageSpy = sinon.spy @view, 'displayMessage'
@confirmUnloadSpy = sinon.spy @view, 'confirmUnload'
@formChangedSpy = sinon.spy @view, 'formChanged'
@view.delegateEvents()
it "success will called when 'ajax:success' is fired and form error has occured", ->
@view.$el.trigger 'ajax:success', ['',{'error','','','error_message'}, '']
expect(@successSpy).toHaveBeenCalled()
it "fail ajax request, will call error method", ->
@view.$el.trigger 'ajax:failure'
expect(@errorSpy).toHaveBeenCalled()
expect(@displayMessageSpy).toHaveBeenCalled()
it 'confirmUnload called when page reloaded', ->
@view.$el.trigger 'beforeunload'
expect(@confirmUnloadSpy).toHaveBeenCalled()
it 'when form changed, formChanged method call', ->
@view.$el.trigger 'change'
expect(@formChangedSpy).toHaveBeenCalled()
但我在茉莉花中出错了
TypeError: Cannot read property 'form' of undefined
所以,我的问题是
为什么我在使用 Backbone.Collection 测试 Backbone.View 时出现错误?>