Skip to content

Instantly share code, notes, and snippets.

@lennym
Created October 24, 2012 10:48
Show Gist options
  • Save lennym/3945437 to your computer and use it in GitHub Desktop.
Save lennym/3945437 to your computer and use it in GitHub Desktop.
Spying on bound methods
describe('A hypothetical situation', function () {
var View = Backbone.View.extend({
initialize: function () {
this.model = new Backbone.Model;
this.model.on('change', this.render, this);
}
}), instance;
beforeEach(function () {
instance = new View();
});
it('view renders on model change', function () {
spyOn(instance, 'render');
instance.model.trigger('change');
expect(instance.render).toHaveBeenCalled(); // fails
});
});
@keithamus
Copy link

Spy on the prototype of the view instead:

    spyOn(View.prototype, 'render');

@lennym
Copy link
Author

lennym commented Oct 24, 2012

That still fails. Changing the event binding to:

this.model.on('change', function () { this.render(); }, this);

works, but is ugly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment