Backbone.js 和 Jasmine Spys 没有被调用

标签 backbone.js jasmine

我试图测试当一个元素被点击时,一个函数被调用。看起来很容易,但我一定错过了一些愚蠢的东西,因为我似乎无法让这个简单的例子工作。

这是我的看法

(function($) {
    window.LaserMonitor = {
        Views: {}
    };

    window.LaserMonitor.Views.WorkstationSummary = Backbone.View.extend({
        tagName: 'li',
        className: 'StationItem',

        initialize: function() {
            _.bindAll(this, 'showDetails');
            this.template = _.template($("#WorkstationSummary").html());
        },

        events: {
            'click h3' : 'showDetails'
        },

        showDetails: function() {
        },

        render: function() {
            var renderedTmpl = this.template(this.model.toJSON());

            $(this.el).append(renderedTmpl);

            return this;
        }
    });
})(jQuery);

这是我的 Jasmine 测试:
describe('WorkstationSummary Item', function() {
    beforeEach(function() {
        _.templateSettings = {
          interpolate: /\{\{(.+?)\}\}/g,
          evaluate: /\{\{(.+?)\}\}/g
        };      

        loadFixtures('LaserMonitorFixture.html');

        this.model = new Backbone.Model({
            id: 1,
            name: 'D8',
            assigned: 1900,
            inProgress: 4,
            completed: 5
        });

        this.view = new window.LaserMonitor.Views.WorkstationSummary({model: this.model});
    });

    describe('Events', function() {
        beforeEach(function() {
            this.view.render();
        });

        it('should trigger click event', function() {
            this.header = this.view.$('h3');

            spyOn(this.view, 'showDetails');

            this.header.click();

            expect(this.view.showDetails).toHaveBeenCalled();
        });
    });
});

本次运行的结果是:

Error: Expected spy on showDetails to have been called. at new (http://localhost:57708/JobMgr2/test-js/lib/jasmine-1.0.2/jasmine.js:102:32) at [object Object].toHaveBeenCalled (http://localhost:57708/JobMgr2/test-js/lib/jasmine-1.0.2/jasmine.js:1171:29) at [object Object]. (http://localhost:57708/JobMgr2/test-js/spec/LaserMonitorSpec.js:33:34) at [object Object].execute (http://localhost:57708/JobMgr2/test-js/lib/jasmine-1.0.2/jasmine.js:1001:15) at [object Object].next_ (http://localhost:57708/JobMgr2/test-js/lib/jasmine-1.0.2/jasmine.js:1790:31) at http://localhost:57708/JobMgr2/test-js/lib/jasmine-1.0.2/jasmine.js:1780:18



编辑:为完整性添加夹具模板:
<script type="text/template" id="WorkstationSummary">
    <h3>{{ name }} ({{ assigned }}/{{ inProgress }}/{{ completed }})</h3>
    <ul>
    </ul>
</script>

最佳答案

如果您为方法创建一个 spy ,在运行测试时,而不是调用实际的方法, spy 会被调用。 spy 是该方法的包装器。但这里的问题是您在创建 spy 之前创建了 View 。所以实际的方法被调用而不是 spy 。您要做的是在创建 View 对象之前创建 spy 。我用过sinon.js监视方法。您必须使用 View 的原型(prototype)来监视该 View 的方法:

var workStationSpy = sinon.spy(window.LaserMonitor.Views.WorkstationSummary.prototype, "showDetails");
this.view = new window.LaserMonitor.Views.WorkstationSummary({model: this.model});
this.view.render();
expect(workStationSpy).toHaveBeenCalled();
workStationSpy.restore();

关于Backbone.js 和 Jasmine Spys 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8085534/

相关文章:

javascript - Backbone Js : . on and .listen vs .bind

jquery - Jasmine jQuery : Using toBeHidden() and toBeVisible()

javascript - Protractor “陈旧元素引用 : element not attached. ..如何解决此问题?

jquery - 为什么 Backbone 的事件 obj 缺少属性?

backbone.js - Backbone JS 按钮单击事件不起作用

javascript - 避免在backbone.js集合中出现Javascript "The local variable may not have been initialized"警告

javascript - 将方法添加到父作用域以测试在父作用域上下文中执行表达式

javascript - 将网页加载到运行 PhantomJS 的 headless Jasmine 规范中

javascript - $event.stopPropogation 不是 Angularjs 单元测试中的函数错误

javascript - 可视化分层数据