我想测试一个模块的行为(使用 Marionette )是否有效 (1)。
奇怪的是,js 模块 (1) 有效,但单元测试 (2) 使用 Jasmine失败。
有什么想法吗?
(1)
/*global define*/
define([
'marionette',
'tasks/views/item',
'text!tasks/templates/list.html',
'collections/tasks'
], function (Marionette, itemView, listTemplate, TaskCollection) {
"use strict";
var ListView = Marionette.CompositeView.extend({
initialize: function () {
this.collection = new TaskCollection();
this.collection.fetch();
},
template: listTemplate,
itemView: itemView,
appendHtml: function (collectionView, itemView) {
collectionView.$el.find('ul.tasks').append(itemView.el);
}
});
return ListView;
});
(2)
// spec file
it("should add a new element", function () {
// TODO
var itemView = new Backbone.View(),
collectionView = new Backbone.View();
this.view.appendHtml(collectionView, itemView);
expect(this.view.$el.find('ul.tasks').length).toEqual(1);
// Expected 0 to equal 1.
});
最佳答案
var itemView = new Backbone.View(),
collectionView = new Backbone.View();
this.view.appendHtml(collectionView, itemView);
抱歉,你想在这里完成什么?在我看来,你现在向 collectionView.$el.find('ul.tasks')
添加了一些内容,而 collectionView.$el
只是空的。所以 collectionView.$el.find('ul.tasks')
什么都不返回等等。
添加 console.log()
来检查:
appendHtml: function (collectionView, itemView) {
console.log(collectionView.$el.html());
collectionView.$el.find('ul.tasks').append(itemView.el);
}
关于javascript - 使用 Marionette.CompositeView 对模块进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11136697/