我有一个假人Backbone.Model
App.Models.Note = Backbone.Model.extend({
default: {
title: ''
}
);
和Backbone.View
我的模型如下:
App.Views.NoteView = Backbone.View.extend({
template: ...,
initialize: function () {
this.listenTo(this.model, "change", this.render);
this.render();
},
render: function () {
this.$el.html(this.template({
title: this.model.get("title")
}));
return this;
}
});
对于测试,我使用 mocha.js + chai + sinon,我有以下测试
describe("App.Views.NoteView", function () {
beforeEach(function () {
this.view = new App.Views.NoteView({
el: this.$fixture,
model: new App.Models.Note()
});
}
afterEach(function () {
this.view.model.destroy();
});
it("my try 1", function () {
var mySpy1 = sinon.spy(this.view, "render");
this.view.model.set({
title: "a new Title"
});
expect(this.view.render).to.have.been.calledOnce;
});
}
我试图测试的是监视该render
方法:当我更改模型属性时,render
将调用该方法。但是,即使渲染正常执行,测试也会给我错误
'expected render to be called once but was called 0 times'
有什么帮助吗?