我正在爲Backbone View編寫測試以測試在獲取模型後調用渲染函數。測試是:Backbone.js查看在瀏覽器中使用Sinon Spies的測試
beforeEach(function() {
$('body').append('<div class="sidebar"></div>');
profileView = new ProfileView();
});
it('should call the render function after the model has been fetched', function (done) {
profileView.model = new UserModel({md5: 'd7263f0d14d66c349016c5eabd4d2b8c'});
var spy = sinon.spy(profileView, 'render');
profileView.model.fetch({
success: function() {
sinon.assert.called(spy);
done();
}
});
});
我使用Sinon Spies將間諜對象附加到profileView視圖對象的渲染函數。
的觀點是:
var ProfileView = Backbone.View.extend({
el: '.sidebar'
, template: Hogan.compile(ProfileTemplate)
, model: new UserModel()
, initialize: function() {
this.model.bind('change', this.render, this);
this.model.fetch();
}
, render: function() {
$(this.el).html(this.template.render());
console.log("Profile Rendered");
return this;
}
});
的獲取是所謂的測試後,改變事件射擊和視圖的渲染功能獲取調用,但興農間諜沒有檢測到渲染正在稱爲失敗。
作爲一個實驗,我試圖調用在測試中呈現功能以查看是否間諜識別它:
it('should call the render function after the model has been fetched', function (done) {
profileView.model = new UserModel({md5: 'd7263f0d14d66c349016c5eabd4d2b8c'});
var spy = sinon.spy(profileView, 'render');
profileView.render();
profileView.model.fetch({
success: function() {
sinon.assert.called(spy);
done();
}
});
});
間諜檢測被叫在上述情況下作出。
有誰知道爲什麼Spy在我的初始測試中沒有識別渲染調用?