我应该如何在backbone.marionette中测试/侦察这种事件

时间:2012-08-21 15:45:41

标签: javascript backbone.js jasmine marionette

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


P.S。:

此问题与此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;

1 个答案:

答案 0 :(得分: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]()