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

标签 javascript backbone.js jasmine marionette

我想测试一个模块的行为(使用 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/

相关文章:

javascript - 在 AJAX 查询 AngularJS 后重新加载 DOM

javascript - 从 MVC3 服务器端传递 backbone.js 的 ViewModel

Javascript 变量作用域行为

codeigniter - 无法使用 Codeigniter、REST、Lighttpd url-rewrite 和 Backbone.js 接收 POST 数据

javascript - 假 Ajax 请求

angular - 使用 HttpClient 在 Angular 中测试文件上传,但不能期望在 FormData 上上传

javascript - Jaeger 后端未接收到任何数据(使用 Jager Node.js 客户端)

javascript - 无法从php文件接收js ajax请求中的json

javascript - Node.js,Socket.io 发出不工作

javascript - Jasmine .toThrow 错误