Jasmine 间谍 callThrough 和 callFake [英] Jasmine spies callThrough and callFake

查看:28
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆