Я пишу test для Backbone View, чтобы проверить, вызывается ли функция рендеринга после выборки модели. Тест::
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;
}
});
После вызова выборки в тесте срабатывает событие изменения и вызывается функция рендеринга представления, но Sinon Spy не обнаруживает, что рендеринг вызывается, и завершается ошибкой.
В качестве эксперимента я попытался вызвать функцию рендеринга в тесте, чтобы увидеть, идентифицировал ли ее шпион.:
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();
}
});
});
Шпион обнаружил, что вызов был сделан в приведенном выше случае.
Кто-нибудь знает, почему шпион не идентифицирует вызов рендеринга в моем первоначальном тесте?