期待间谍,但得到了函数 [英] Expected a spy, but got Function

查看:24
本文介绍了期待间谍,但得到了函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为此模块 (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.

这篇关于期待间谍,但得到了函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆