我的测试声称将数据插入到字符串中,但实际上还会检查模型是否自动更新DOM。我该如何处理这种情况?
使用Jasmine测试ItemView:
describe("ItemView handles rendering views with a model", function() {
it("should render a template and interpolate data from a model", function() {
var user = new Backbone.Model({
name : "Peeter",
age : 24
});
var itemView = new ItemView({
model : user,
template : 'Hello my name is <%= model.get("name") %> and I\'m <%= model.get("age") %> years old.'
});
itemView.render();
user.set({
name : "Pjotr",
age : 25
});
expect(itemView.$el).toHaveText('Hello my name is Pjotr and I\'m 25 years old.');
});
});
*正在测试的代码(BaseView有自己的测试)*
我的基本观点:
define(['jquery', 'backbone', 'underscore'], function($, Backbone, _) {
'use strict';
var BaseView = Backbone.View.extend({
/**
* Update the dom element
*/
replaceDomElement : function() {
this.trigger("before:replace_dom_element");
var $el = this.$el.empty();
var $clone = $el.clone();
$el.replaceWith($clone);
this.setElement($clone);
this.trigger("after:replace_dom_element");
},
/**
* Default serialize function
* @returns empty object
*/
serialize: function(){
return {};
},
/**
* Render the view, passing the pictures collection to the view and the
* inner template
*/
render : function() {
this.replaceDomElement();
this.trigger("before:render");
var template = _.template(this.options.template);
/**
* Allow for serialization overwriting
*/
var options = this.serialize();
if(this.options.serialize) {
options = _.isFunction(this.options.serialize) ? this.options.serialize() : this.options.serialize;
}
this.$el.append(template(options));
this.trigger("after:render");
}
});
return BaseView;
});
我的项目视图:
define(['jquery', 'yolojs/views/BaseView'], function($, BaseView) {
'use strict';
var ItemView = BaseView.extend({
className : "item",
/**
* Helper function to determine which data should the view pass to the template
*/
serialize : function() {
return {
model : this.model
};
},
/**
* Set up our view to autoupdate on change
*/
initialize: function() {
/**
* Assign the cid as a data attribute on the model
*/
this.$el.attr("data-cid", this.model.cid);
this.model.on('change',this.render,this);
}
});
return ItemView;
});
答案 0 :(得分:1)
最简单的方法是:
describe("ItemView", function() {
var user;
var itemView;
beforeEach(function() {
user = new Backbone.Model({
name : "Peeter",
age : 24
});
itemView = new ItemView({
model : user,
template : 'Hello my name is <%= model.get("name") %> and I\'m <%= model.get("age") %> years old.'
});
});
it("should render a template and interpolate data from a model", function() {
itemView.render();
expect(itemView.$el).toHaveText('Hello my name is Peeter and I\'m 24 years old.');
});
it("should call render if the models data changes", function() {
spyOn(itemView, 'render');
user.set({
name : "Pjotr",
age : 25
});
expect(itemView.render).toHaveBeenCalled();
});
it("should update the dom element with the new models data", function() {
user.set({
name : "Pjotr",
age : 25
});
expect(itemView.$el).toHaveText('Hello my name is Pjotr and I\'m 25 years old.');
});
});
由于以下原因,我还需要更改我的项目视图:Testing backbone.js application with jasmine - how to test model bindings on a view?
这个答案归功于@theml