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

Lor*_*ard 25 javascript backbone.js jasmine marionette

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


(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.
    });
});
Run Code Online (Sandbox Code Playgroud)

(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
});
Run Code Online (Sandbox Code Playgroud)

ant*_*njs 41

你需要进入实际的方法,在这种情况下是在原型上.

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();
    });
});
Run Code Online (Sandbox Code Playgroud)

监视原型是一个很好的技巧,当你无法访问想要监视的实际实例时,你可以使用它.