1

我正在尝试为此模块 (2) 实施测试 (1)。
我的目的是检查在触发特定事件时是否获取了集合。
正如您从我在 (2) 中的评论中看到的那样,我收到消息 Expected spy restartPolling to have been called.
The module works but the test failed。有任何想法吗?


PS:

This question is related to this one 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;
4

1 回答 1

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]()
于 2012-08-21T20:42:20.380 回答