title: Spy on Backbone.js View Functions with Jasmine
body: <p>I'm trying to write a Jasmine spec to verify that a view's function is called when a model is added to the view's collection.</p>
  
  <p>In the view's initialize function I do</p>
  
  <pre><code>this.collection.on('add', this.fooAdded, this);
  </code></pre>
  
  <p>In my Jasmine spec I'm doing:</p>
  
  <pre><code>describe('Foo View', function() {
     it('should call fooAdded when a Foo is added', function() {
        var view = new FooView({collection: new FooCollection()});
        spyOn(view, 'fooAdded').andCallThrough();
        view.delegateEvents();
        view.collection.add({name: 'foo'});
        expect(view.fooAdded).toHaveBeenCalled();
     });
  });
  </code></pre>
  
  <p>My implementation of fooAdded() logs something to the console, so I know it's being called. However the spy doesn't see that the fooAdded() has been called.</p>
  
  <p>See my <a href="http://jsfiddle.net/erturne/j8YuM" rel="nofollow" title="jsFiddle">jsFiddle</a></p>