Unit testing 如何在Jasmine忽略包含的逻辑部分的情况下测试主干视图?
我尝试用jasmine测试主干视图的正确模板分配 这是我的测试:Unit testing 如何在Jasmine忽略包含的逻辑部分的情况下测试主干视图?,unit-testing,view,backbone.js,jasmine,Unit Testing,View,Backbone.js,Jasmine,我尝试用jasmine测试主干视图的正确模板分配 这是我的测试: describe("Backbone views", function() { // Runs before every View spec beforeEach(function() { // Instantiates a new View instance this.view = new Index(); }); it("should contain the appropriate template",
describe("Backbone views", function() {
// Runs before every View spec
beforeEach(function() {
// Instantiates a new View instance
this.view = new Index();
});
it("should contain the appropriate template", function() {
expect(this.view.template).toEqual(IndexViewTemplate);
});
}
view.template变量填充在渲染函数中:
initialize: () ->
super()
@render()
# Renders the view's template to the UI
render: () ->
# Setting the view's template property using the Underscore template method
@template = _.template template,
{}
# Dynamically updates the UI with the view's template
@$el.html @template
# Maintains chainability
return @
变量IndexViewTemplate包含原始模板代码,其中包含逻辑内容,如
当我运行代码时,我得到一个异常,这两个元素不相等,因为在这个.view.template中,逻辑部分是。。。消失了……;):
应包含适当的模板
预期
“作者”
相等
“0){%>由编写。”。
测试作业的最佳方法是什么
敬请注意,hijolan呈现模板的方式有点不习惯。通常不需要像在这里的
@template
属性中那样保留“呈现的”模板字符串:
@template = _.template template,
viewConfig
@$el.html @template
我个人会将未渲染的模板分配给初始化
构造函数中的@template
属性,假设该模板在视图实例的生存期内没有更改:
initialize: () ->
super()
@template = template
@render()
# Renders the view's template to the UI
render: () ->
viewConfig = _.merge {}, @config.template, {}
@$el.html _.template(@template, viewConfig)
return @
之后,您的测试expect(this.view.template).toEqual(IndexViewTemplate)
测试您希望它测试的内容-是否已分配正确的模板
顺便说一句,我真的不明白这行代码的作用:
viewConfig = _.merge {}, @config.template, {}
呈现模板的方式有点不习惯。通常不需要像在
@template
属性中那样保留“呈现的”模板字符串:
@template = _.template template,
viewConfig
@$el.html @template
我个人会将未渲染的模板分配给初始化
构造函数中的@template
属性,假设该模板在视图实例的生存期内没有更改:
initialize: () ->
super()
@template = template
@render()
# Renders the view's template to the UI
render: () ->
viewConfig = _.merge {}, @config.template, {}
@$el.html _.template(@template, viewConfig)
return @
之后,您的测试expect(this.view.template).toEqual(IndexViewTemplate)
测试您希望它测试的内容-是否已分配正确的模板
顺便说一句,我真的不明白这行代码的作用:
viewConfig = _.merge {}, @config.template, {}
在何处/如何分配
视图.template
属性?在渲染函数中…我在上面添加了它…在渲染函数中…我在上面添加了它…谢谢你的回答!关于你的最后一个问题:它是我以前尝试过的东西的遗物。它在Dome子类中用于m以正确的方式使用两个配置的变量和父类的变量谢谢你的回答!关于你的最后一个问题:它是我以前尝试过的东西的残余。它用于Dome子类中,以正确的方式合并两个配置的变量和父类的变量