Javascript 测试主干时出现jasmine Get错误无法读取属性

Javascript 测试主干时出现jasmine Get错误无法读取属性,javascript,backbone.js,jasmine,Javascript,Backbone.js,Jasmine,我编写Backbone.Collection来加载JSON文件并在Backbone.View中使用 这是我的脊梁骨。收藏 class Backbone.Config extends Backbone.Collection initialize: (filename = 'settings') -> @_load(filename) # private load setting file _load: (filename) -> @fetch

我编写Backbone.Collection来加载JSON文件并在Backbone.View中使用

这是我的脊梁骨。收藏

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]
我把它用在我的脊梁上

像这样

 error: ->
     @displayMessage @config.get().form.display_message.error, 'red', 3000
这很有效,但当我用jasmine编写测试时

描述‘事件’,->

 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
所以,我的问题是


为什么我在测试主干时出错。在其中使用主干查看。集合?>

(1)为什么配置是一个集合?(2) 集合已具有
get
方法,重写该方法可能会导致问题。(3) 当你调用
@config.get()
时,是什么让你认为
fetch
(一个AJAX调用)已经获得了它的数据呢?(1)我想在js中收集神奇的数字并使用它(2)i c!thx,我会修复它(3)实例化时加载了json数据,但是
@fetch
是一个AJAX调用,
get
返回
未定义的
闻起来像是“AJAX尚未完成”问题。如果用一个简单的内联赋值替换
@fetch
调用以强制所有事情同步发生,会发生什么?thx,我解决了:)确切地说,ajax返回“readyState:1”,因为ajax没有完成。所以我写了
async:false
,测试成功了:)