angularjs - 如何监视也被属性引用的函数
问题描述
鉴于此代码:
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
被监视的,而不是函数。我怎样才能解决这个问题?
解决方案
对于这种情况,您可以使用rewire
. 使用rewire
您可以替换任何未导出的模块中的函数/变量
推荐阅读
- vb.net - 如何增加字符串中存在的数值
- gatsby - 使用 transifex 进行 Gatsby 翻译
- javascript - 刷新页面并关闭浏览器回调
- hybris - 如何在 Hybris 中设置订单限制?
- angular - 在formGroup中创建新的子formGroup - Angular 6
- swift - 如何动态修改 SKShapeNode 的路径?
- javascript - 在可见元素上过滤/搜索表格 html
- http-proxy-middleware - http-proxy-middleware 仅修改端口
- javascript - 为什么 firestore / AngularFire SDK 会为一个请求打包多个 Promise?
- java - 我的 Jar 文件无法打开,我解决问题的动力正在消亡