angularjs - 需要一些 Jasmine 中 "callThrough"和 "callFake()"的实际示例

标签 angularjs jasmine karma-jasmine

我正在学习实现 karma & Jasmine在 AngularJS 中,我将通过一些示例来更好地理解它。
我对 callThrough 有点困惑.
如果理解有误,请指正,与callFake()略有相似之处以某种方式,我们实际上并未调用该函数。在 callFake()我们还提供了一个具有返回类型但不在 callThrough 中的函数.
来自 Jasmine 文档:

By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.


请对此有所了解。
更新:
苦苦挣扎后,我意识到一篇文章值得分享。 Here is a Medium article to anyone who stumbles across this question

最佳答案

你的理解看起来不错:
spy :and.callThrough

By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.


spy :and.callFake

By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied function.

If the function being spied on receives arguments that the fake needs, you can get those as well


加号:当您添加 callThrough 时。您不仅在跟踪 spy 的电话。您还可以调用该方法来测试它。这里的方法实际上是被调用的。在 callFake 时,您只能通过检查参数来测试它是否被正确调用。没有调用真正的方法。这是有道理的,因为它被称为 fake call

关于angularjs - 需要一些 Jasmine 中 "callThrough"和 "callFake()"的实际示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414712/

相关文章:

Angular 4 : Mock ElementRef

javascript - Angular 7 测试 : NullInjectorError: No provider for ActivatedRoute

javascript - Jasmine beforeEach 不等待完成回调

javascript - 从多个 Controller 更新服务中的变量

javascript - 使用 $scope 变量的值来指定属性

intellij-idea - 如何在 IntelliJ IDEA 中运行单个 Jasmine 测试

unit-testing - 如何使用 jasmine 对我的自定义指令进行单元测试?

angularjs - TeamCity : PhantomJS have not captured in 60000 ms, 查杀

angularjs - 动态更新 AngularJS 指令中的 ion.rangeSlider ngModel

javascript - 在 angularjs/javascript 中操作数组