我应该如何在backbone.mariente中测试/监视此类事件

How should I test/spy this kind of event in backbone.marioentte

本文关键字:监视 事件 测试 backbone mariente 我应该      更新时间:2023-09-26

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


p.S.:

这个问题与这个有关预期是间谍,但得到了功能


(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;

我正在使用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]()