首页 > 解决方案 > 如何监视也被属性引用的函数

问题描述

鉴于此代码:

function getAnimal(type, color) {
    console.log('blub'); // this is triggered when executing the test
}
this.getAnimal = getAnimal;

这个测试代码:

describe('getAnimal()', function() {
    it('should get an animal based on type and color', function() {
        spyOn(this.AnimalService, 'getAnimal');
        this.AnimalService.getAnimal();

        expect(this.AnimalService.getAnimal).toHaveBeenCalled();
    });
});

但我收到此错误消息: Expected spy function(){return i} to have been called。

所以我猜只有this.getAnimal = getAnimal被监视的,而不是函数。我怎样才能解决这个问题?

标签: angularjstestingjasminespyspyon

解决方案


对于这种情况,您可以使用rewire. 使用rewire您可以替换任何未导出的模块中的函数/变量


推荐阅读