Tôi đang viết một thử nghiệm cho Chế độ xem xương sống để kiểm tra hàm render được gọi sau khi tìm nạp mô hình. Bài kiểm tra là:Kiểm tra chế độ xem Backbone.js bằng cách sử dụng Sinon Spies trong trình duyệt
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();
}
});
});
Tôi đang sử dụng Sinon Spies để đính kèm một đối tượng gián điệp vào hàm render của đối tượng ViewView.
Quan điểm là:
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;
}
});
Sau Truy xuất được gọi trong các thử nghiệm, sự kiện thay đổi được bắn và của render xem chức năng là nhận được gọi, nhưng Sinon Spy không phát hiện rằng làm đang được gọi và thất bại.
Là một thử nghiệm, tôi đã cố gắng gọi render chức năng trong các thử nghiệm để xem nếu Spy xác định nó:
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();
}
});
});
The Spy phát hiện được gọi đã được thực hiện trong trường hợp trên.
Có ai biết tại sao gián điệp không nhận dạng được lệnh gọi kết xuất trong thử nghiệm ban đầu của tôi không?