期待一个间谍,但得到了功能

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

I am trying to implement a test (1) for this module (2).
My purpose is to check if the collection is fetched when a particular event is triggered.
As you can see from my comment in (2) I get the message Error: Expected a spy, but got Function.
The module works but the test fails. any ideas?

(1)

// jasmine test module

describe('When onGivePoints is fired', function () {
    beforeEach(function () {
        spyOn(this.view.collection, 'restartPolling').andCallThrough();
        app.vent.trigger('onGivePoints');
    });
    it('the board collection should be fetched', function () {
        expect(this.view.collection.restartPolling).toHaveBeenCalled();
       // Error: Expected a spy, but got Function.
    });
});

<小时>

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

推荐答案

你需要进入实际的方法,在这个例子中是在原型上.

You need to get into the actual method, which in this case is on the prototype.

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

监视原型是一个不错的技巧,当您无法到达要监视的实际实例时,可以使用它.

Spying on the prototype is a nice trick you can use when you can't get to the actual instance you want to spy on.

相关文章