我正在尝试为此模块(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;
答案 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]()