2011-12-07 61 views
0

我有一個骨幹視圖一個相當簡單的一套規範的:期待的功能與功能調用作爲參數

describe 'Avia.MatricesView', -> 

    beforeEach -> 
    @model = { 
     bind: -> 
     fetch: -> 
    } 
    spyOn(Avia, 'Matrices').andReturn(@model) 
    @matricesView = new Avia.AviaView(addFixtureDiv('fixture')) 

    describe 'initialization', -> 

    beforeEach -> 
     spyOn(@model, 'bind') 
     spyOn(@model, 'fetch') 
     @matricesView.initialize() 

    it 'creates a new Matrices model', -> 
     expect(Avia.Matrices).toHaveBeenCalledOnce() 

    it 'binds the model change event to render', -> 
     expect(@model.bind).toHaveBeenCalledWith('change', @matricesView.render) 

    it 'fetches the model data', -> 
     expect(@model.fetch).toHaveBeenCalledWith(success: @matricesView.render, error: @matricesView.showError) 

的MatricesView不一樣的規格預計:

initialize: => 
    @model = new Avia.Matrices() 
    @model.bind('change', @render) 
    @model.fetch(success: @render, error: @showError) 

showError: => 
    alert('An error occurred while fetching data from the server.') 

render: => 
    html = JST['views/matrices_view_template']() 
    @el.html(html) 

的期望一個新的矩陣模型正在創建通過。其他兩個規格失敗,不過,在這讓我困惑的方法:

Avia.MatricesView initialization binds the model change event to render. (/home/duncan/avia/spec/javascripts/views/matrices_view_spec.js.coffee:21) 
    Expected spy bind to have been called with [ 'change', Function ] but was called with [ [ 'change', Function ] ] (line ~22) 
    expect(this.model.bind).toHaveBeenCalledWith('change', this.matricesView.render); 

Avia.MatricesView initialization fetches the model data. (/home/duncan/avia/spec/javascripts/views/matrices_view_spec.js.coffee:24) 
    Expected spy fetch to have been called with [ { success : Function, error : undefined } ] but was called with [ [ { success : Function, error : Function } ] ] (line ~25) 
    expect(this.model.fetch).toHaveBeenCalledWith({ 

據我所知,茉莉花認爲,通過@matricesView.render在規範的範圍內返回的功能是由@render返回的功能不同在MatricesView的實例範圍內。

另外,我完全不明白爲什麼@matricesView.showError在MatricesView中明確定義時未定義。

任何幫助將不勝感激。我肯定需要第二雙眼睛在這個像我有點厭倦現在: -/

回答

1

對,我是真的現在很尷尬。望着這與新鮮一雙眼睛在早晨:

@matricesView = new Avia.AviaView(addFixtureDiv('fixture')) 

...應該是...

@matricesView = new Avia.MatricesView(addFixtureDiv('fixture')) 

測試應該已經發生故障,因爲我實際上是測試錯誤的類。

o_O

+0

哈哈,我一直在爲今天的大多數尋找答案,也沒有發現它!幹得好的隊友,感謝您更新問題 – StevenMcD

0

第一個失敗的測試似乎是與此相關的問題:https://github.com/pivotal/jasmine/issues/45嘗試包裝你的論點中的數組:

expect(@model.bind).toHaveBeenCalledWith(['change', @matricesView.render]) 

第二個是更莫名其妙 - 沒有辦法@matricesView.showError是未定義的(你可以投入console.log來確認這一點)。所以這可能只是一個字符串化問題。嘗試製作簡化的測試用例併發布到Jasmine問題跟蹤器。但就測試通過而言,嘗試使用數組包裝。如果這不起作用,難道茉莉花正在測試參考平等而不是深度物體平等嗎?如果是這樣的話,你可能想試試最近添加的objectContaining匹配器。