My code is functioning properly, but my test case is failing for one of its expectations. I don't understand why my spy fails to believe that a method has been executed.
I bind an event listener like so:
var playlists = Backbone.Collection.extend({
initialize: function(){
this.on('add', this._onAdd);
},
// Method:
_onAdd: function (model, collection, options) {
console.log('onAdd');
this._foo();
},
_foo: function(){
console.log('foo');
}
});
Playlists = new playlists();
and I am using sinon to spy on my object:
it('should call _onAdd when adding a model to the collection', function() {
sinon.spy(Playlists, '_onAdd');
sinon.spy(Playlists, '_foo');
Playlists.add({});
expect(Playlists._onAdd.calledOnce).to.equal(true);
expect(Playlists._foo.calledOnce).to.equal(true);
Playlists._onAdd.restore();
Playlist._foo.restore();
});
My test case fails because the expectation for _onAdd
being called once is not true. However, the expectation for _foo
being called once is true.
I am doing something incorrect with how I am spying on my event listener. Why does sinon not believe _onAdd
has been called. How can I correct this?