バックボーン ビューがあり、ある要素のクリック イベントがその要素にバインドされた関数を呼び出すことを確認するテストを作成したいと考えています。私の見解は次のとおりです。
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');
}
});
私の仕様は次のとおりです。
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();
});
ただし、上記のビュー定義はプロトタイプprotoでanswerYes メソッドを作成しますが、スパイはビュー内の実際のインスタンスで関数を作成するため、スパイである view.answerYes() と view.__proto__.answerYes, になります。これは私が実際にスパイしたいものです。
ビュー定義の answerYes メソッドをオーバーライドするようにスパイを作成するにはどうすればよいですか?