预计间谍,却得到了功能 [英] Expected a spy, but got Function

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

问题描述

我想实现一个试验(1)该模块(2)。结果
我的目的是检查,如果在触发特定事件的集合是牵强。结果
你可以从我的(2)我得到的消息错误评论看:预计间谍,却得到了功能结果。
该模块工作,但测试失败。有任何想法吗?


(1)

  //茉莉测试模块描述(当onGivePoints被激发',函数(){
    beforeEach(函数(){
        spyOn(this.view.collection,'restartPolling')andCallThrough()。
        app.vent.trigger('onGivePoints');
    });
    它('板收集应取',函数(){
        期待(this.view.collection.restartPolling).toHaveBeenCalled();
       //错误:预期间谍,却得到了功能。
    });
});


(2)

  //模型视图模块
返回Marionette.CompositeView.extend({
    初始化:功能(){
        this.collection =新UserBoardCollection();
        this.collection.startPolling();
        app.vent.on('onGivePoints',this.collection.restartPolling);
    },
    //其他code
});


解决方案

您需要进入实际的方法,在这种情况下是在原型。

 描述(当onGivePoints被激发',函数(){
    beforeEach(函数(){
        spyOn(UsersBoardCollection.prototype,'restartPolling')andCallThrough()。
        app.vent.trigger('onGivePoints');
    });
    它('板收集应取',函数(){
        期待(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
    });
});

在原型间谍是一个很好的技巧,当你不能得到你想要刺探的实际情况下,你可以使用。

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天全站免登陆