Backbone jasmine sinon.stub 类型错误
我正在尝试为 Backbone Todos 集合编写一个简单的规范,用于存根 Backbone Todo 模型。
这是我的规范:
describe "TodoApp.Collections.Todos", ->
beforeEach ->
@todoStub = sinon.stub window, 'TodoApp.Models.Todo'
afterEach ->
@todoStub.restore()
这给了我以下错误:
TypeError: Attempted to wrap undefined property TodoApp.Models.Todo as function
Todo 模型被定义为 todo = new TodoApp.Models.Todo() 不会给出错误。
这是范围界定问题吗?有人能指出我正确的方向吗?
I'm trying to write a simple spec for a Backbone Todos collection which stubs the Backbone Todo model.
Here's my spec:
describe "TodoApp.Collections.Todos", ->
beforeEach ->
@todoStub = sinon.stub window, 'TodoApp.Models.Todo'
afterEach ->
@todoStub.restore()
This gives me the following error:
TypeError: Attempted to wrap undefined property TodoApp.Models.Todo as function
The Todo model is defined though as todo = new TodoApp.Models.Todo() doens't give an error.
Is it a scoping issue? Could somebody point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚也遇到了这个问题。你应该这样称呼它......
而不是这样。
这为我解决了问题
@smek:这也解决了您的问题 http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html
I just ran into that problem too. You should call it like this...
instead of this.
this solved the problem for me
@smek: this also solves your problem from http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html
您使用的语法
sinon.stub window, 'TodoApp.Models.Todo'
将用于将window['TodoApp.Models.Todo']
包装为函数。 http://sinonjs.org/docs/#stubs有了 sinon,你更有可能成为使用存根将特定函数包装在 Todo 模型上:
sinon.stub TodoApp.Models.Todo, 'Foo'
。Sinon 可以存根整个对象,但我认为它的设计更加精细。
The syntax you're using
sinon.stub window, 'TodoApp.Models.Todo'
would be for wrappingwindow['TodoApp.Models.Todo']
as a function. http://sinonjs.org/docs/#stubsWith sinon you're more likely going to be wrapping a particular function on your Todo model with a stub:
sinon.stub TodoApp.Models.Todo, 'Foo'
.Sinon can stub an entire object but I think it's designed to be more granular.