【问题标题】:How should I test/spy this kind of event in backbone.marioentte我应该如何在backbone.marionette 中测试/监视这种事件
【发布时间】:2012-08-17 00:22:52
【问题描述】:

我正在尝试为此模块 (2) 实施测试 (1)。
我的目的是检查触发特定事件时是否提取了集合。
从我在 (2) 中的评论中可以看出,我收到了消息 Expected spy restartPolling to have been called.
该模块工作,但测试失败。有任何想法吗?


PS:

这个问题和这个Expected a spy, but got Function有关


(1)

// jasmine test module

describe('When onGivePoints is fired', function () {
    beforeEach(function () {
        spyOn(UsersBoardCollection.prototype, 'restartPolling').andCallThrough();
        app.vent.trigger('onGivePoints');
    });
    it('the board collection should be fetched', function () {
        expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
        // Expected spy restartPolling to have been called.
    });
});

(2)

// model view module
return Marionette.CompositeView.extend({
    initialize: function () {
        this.collection = new UserBoardCollection();
        this.collection.startPolling();
        app.vent.on('onGivePoints', this.collection.restartPolling);
    },
    // other code
});

(3)

// polling module

var poller = {
    restartPolling: function () {
        this.stopPolling();
        this.startPolling(options);
    },
    // other code
};

(4)

var UsersBoardCollection = Backbone.Collection.extend({
    // some code
});

_.extend(UsersBoardCollection.prototype, poller);

return UsersBoardCollection;

【问题讨论】:

    标签: javascript backbone.js jasmine marionette


    【解决方案1】:

    我正在使用 Marionette,我对其进行了一些不同的测试。我的想法不是测试,当事件在调用函数的真实应用程序上触发时,因为这将由 Marionette 开发人员进行测试。

    我测试该事件是否绑定到app.vent。因此,我在app.vent.on 上创建了一个间谍,然后我自己触发了该功能:

    spyOn(app.vent, 'on');
    expect(app.vent.on.argsForCall[0][0]).toBe('onGivePoints')
    
    app.vent.trigger.argsForCall[0][1]()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多