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?

有帮助吗?

解决方案

I just ran into that problem too. You should call it like this...

    beforeEach ->
            @todoStub = sinon.stub window.TodoApp.Models, 'Todo'

instead of this.

    beforeEach ->
            @todoStub = sinon.stub window, 'TodoApp.Models.Todo'

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

其他提示

The syntax you're using sinon.stub window, 'TodoApp.Models.Todo' would be for wrapping window['TodoApp.Models.Todo'] as a function. http://sinonjs.org/docs/#stubs

With 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top