2011-12-07 63 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匹配器。