Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone jasmine sinon.stub typeError

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?

like image 771
smek Avatar asked Feb 22 '12 11:02

smek


2 Answers

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

like image 53
pabera Avatar answered Nov 16 '22 18:11

pabera


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.

like image 34
Eric Bock Avatar answered Nov 16 '22 19:11

Eric Bock