使用Marionette.CompositeView对模块进行单元测试
UnitTest for a module using Marionette.CompositeView
我想测试一个模块(使用木偶)的行为,它的工作(1)。
奇怪的是,js模块(1)工作,但使用Jasmine的单元测试(2)失败了。
有什么想法吗?
(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;
});
(二)
// 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);
}
相关文章:
- 如何为多个需求模块设置单元测试
- 单元测试节点模块与mocha,模块变量行为怪异
- 单元测试使用全局的模块.XMLHttpRequest
- 模块模式单元测试
- 在对揭示模块进行单元测试时,如何存根私有函数
- 用于单元测试的节点.js模块
- 使用Marionette.CompositeView对模块进行单元测试
- 如何在单元测试之间重置 requireJS 模块
- 使用磁带/ES6 单元测试测试 AMD 模块
- 如何使用 sinon.stub 编写单元测试以“请求”节点模块
- 单元测试有状态 RequireJS 模块的模式
- 在单元测试中重置模块
- 模块中的单元测试函数
- 节点.js单元测试:测试fs模块抛出错误时的行为
- 包括mocha单元测试中的模块
- 如何获得'模块'Jasmine单元测试中的TypeScript
- 在使用 Jasmine 进行单元测试时,javascript 揭示模块模式的缺点是什么?
- 使用$httpBackend+$资源进行角因果报应单元测试,而不使用模块进行自举
- 为创建Json REST服务的模块编写单元测试
- 单元测试模块化javascript