I have a backbone view and I want to create a test to confirm that a click event on some element will call the function bound to that element. My view is:
PromptView = Backbone.View.extend({
id:"promptPage",
attributes:{
"data-role":"page",
"data-theme":"a"
},
events:{
"click #btnYes": "answerYes",
"tap #btnYes": "answerYes"
},
render: function(){
$(this.el).html(_.template($('#promptPage-template').html(), this.model.toJSON()));
return this;
},
answerYes: function(){
alert('yes');
}
});
My spec is:
beforeEach(function() {
model = new PromptModel;
view = new PromptView({model:model});
loadFixtures('promptPage.tmpl');
});
it("should be able to answer a question with yes", function() {
var button = $("#btnYes", view.render().el);
expect(button.length).toBe(1);
spyOn(view, 'answerYes');
button.click();
expect(view.answerYes).toHaveBeenCalled();
});
However the above view definition creates the answerYes method on the prototype proto , but the spy creates a function on the actual instance in the view, so I end up with a view.answerYes() which is the spy and view.__proto__.answerYes, which is the one I actually want to spy on.
How can I create a spy so that it overrides the answerYes method of the view definition?