Javascript 期待间谍,但得到了函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12053799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:00:30  来源:igfitidea点击:

Expected a spy, but got Function

javascriptbackbone.jsjasminemarionette

提问by Lorraine Bernard

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?

我正在尝试为此模块 (2) 实施测试 (1)。
我的目的是检查在触发特定事件时是否获取了集合。
正如您在 (2) 中的评论中所见,我收到消息 Error: Expected a spy, but got Function.
The module works but the test failed。有任何想法吗?



(1)

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

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

回答by antonjs

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.

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

回答by Tabish

I was also getting the same issue but I resolved it by passing an argument in the function call. Then you have to write your test case like this in the it

我也遇到了同样的问题,但我通过在函数调用中传递一个参数来解决它。然后你必须像这样写你的测试用例it

var data = {name:"test"}
spyOn(UsersBoardCollection.prototype, "restartPolling").and.callThrough();
UsersBoardCollection.prototype.restartPolling(data);
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();

回答by bnolan

I had this bug because I had two versions of sinon loaded, or possibly i wasn't initialising sinon-jasmine correctly. When I explicitly loaded sinon and then sinon jasmine in my spec setup, it started running correctly.

我有这个错误是因为我加载了两个版本的 sinon,或者我没有正确初始化 sinon-jasmine。当我在规范设置中显式加载 sinon 和 sinon jasmine 时,它​​开始正常运行。