期望以函数作为参数调用函数
Expecting functions to be invoked with functions as parameters
我有一组相当简单的骨干视图规范:
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)
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({
据我所知,Jasmine认为@matricesView.render
在规范范围内返回的函数与@render
在MatricesView实例范围内返回的函数不同。
另外,我完全无法理解为什么@matricesView.showError
在MatricesView中明确定义时未定义。
任何帮助都将非常感激。我肯定需要另一双眼睛,因为我的眼睛现在有点累了:-/
对,我现在真的很尴尬。早晨用一双新鲜的眼睛看着这个:
@matricesView = new Avia.AviaView(addFixtureDiv('fixture'))
…
@matricesView = new Avia.MatricesView(addFixtureDiv('fixture'))
测试应该失败,因为我实际上测试了错误的类。
o_O
第一个失败的测试似乎与这个问题有关:https://github.com/pivotal/jasmine/issues/45尝试将参数包装在数组中:
expect(@model.bind).toHaveBeenCalledWith(['change', @matricesView.render])
第二个更令人困惑—@matricesView.showError
不可能是未定义的(您可以添加console.log
来确认这一点)。所以这可能只是一个弦化问题;尝试生成一个简化的测试用例并将其发布到Jasmine问题跟踪器。但是,要使测试通过,请尝试数组包装。如果这不起作用,可能是Jasmine在测试引用相等性,而不是深度对象相等性?如果是这种情况,您可能想尝试最近添加的objectContaining匹配器。
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- AngularJS:我可以跳过函数参数回调吗
- 为什么不'我们在javascript中使用函数参数的数据类型
- 使用闭包共享构造函数参数
- 函数参数的值错误
- 当一个重要的构造函数参数丢失时应该发生什么
- 阻止ReSharper将JavaScript函数参数放到新行中
- 根据函数参数将CSS值存储在var中,然后对其进行操作
- 使构造函数参数具有ES6类方法的特权
- 当客户端将两个按钮的javascript函数参数修改为相同时,SQL/PHP会更新多行
- Windows.Networking.EndpointPair构造函数参数
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- 大括号中的Javascript函数参数
- jQuery 无法使用 AJAX 调用访问函数内部的函数参数
- 将JSON字符串转换为函数参数
- eval()读取函数参数有多糟糕
- Javascript不能在一个命令中使用函数参数
- 函数参数在内部不可见,返回新的Promise(函数(resolve,reject).传递给被promise化的函数的
- ES6类构造函数参数
- 是否可以在javascript函数参数列表中调用多个函数