使用Marionette.CompositeView对模块进行单元测试

UnitTest for a module using Marionette.CompositeView

本文关键字:单元测试 模块 Marionette CompositeView 使用      更新时间:2023-09-26

我想测试一个模块(使用木偶)的行为,它的工作(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);
}