Jasmine spies callThrough and callFake(Jasmine 间谍 callThrough 和 callFake)
问题描述
我有一个场景,我想在调用回调之后在 beforeEach
上调用 done()
.
I have a scenario whereby I want to call done()
on a beforeEach
after a callback has been invoked.
我尝试这样做:
spyOn(scope, 'onAdmin').and.callThrough().and.callFake(function(){done()})
但我不确定我的行为是否正确.本质上,我想要实现的是能够在每个回调完成后调用 done()
.
But I'm not sure I get the right behaviour. Essentially what I want to achieve is to be able to call done()
after each callback is done doing what it does.
更新:解决方法
scope.onAdminBackup = scope.onAdmin;
spyOn(scope, 'onAdmin').and.callFake(function(admin) {
scope.onAdminBackup();
done() ;
})
推荐答案
我从来没有将这些类型的函数链接在一起,因为在我看来它们似乎做相反的事情.你是说当我调用这个方法 -onAdmin - 在范围内正常调用它.这就是 jasmine 为我们提供的 callThrough 方法所做的.
I have never chained these kinds of functions together cuz in my mind they seem to do the opposite. You are saying when I call this method -onAdmin - in the scope call it as normal. Which is what the callThrough method jasmine provides for us does.
但是你也链接了一个 callFake 方法,所以你说但实际上并没有调用它,而是调用这个假函数 - 非常矛盾.
But then you are chaining along a callFake method as well so then you say but dont actually call it call this fake function instead - very conflicting.
如果你想在 onAdmin 方法上调用 spy,而不是让它被解雇,你希望它做其他事情——一些被嘲笑的事情——然后使用 .and.callFake(fn).还要考虑到上面提到的@stefan - 不要调用函数 - callFake 只是想要一个函数作为参数,它会自行调用它.
If you want to call spy on the method onAdmin and instead of it being fired you want it to do something else - something mocked - then use the .and.callFake(fn). Also take into account like @stefan above said - dont invoke the function - callFake is simply wanting a function as a parameter it will take care of calling it itself.
如果没有向我们展示更多代码,这可能更符合您的要求.
This might be more along the lines of what you are looking for, if not show us some more code.
spyOn(scope, 'onAdmin')and.callFake(done)
这篇关于Jasmine 间谍 callThrough 和 callFake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jasmine 间谍 callThrough 和 callFake
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01