Jasmine 间谍 callThrough 和 callFake

Jasmine spies callThrough and callFake(Jasmine 间谍 callThrough 和 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)