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

查看:350
本文介绍了Jasmine spies callThrough和callFake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我希望在调用回调后在 beforeEach 上调用 done()

I have a scenario whereby I want to call done() on a beforeEach after a callback has been invoked.

我尝试按如下方式执行此操作:

I tried to do this as follows :

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时 - 在范围内调用它是正常的。这就是茉莉花为我们提供的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上调用间谍而不是它被解雇你想让它做其他的东西 - 嘲笑的东西 - 然后使用.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 spies callThrough和callFake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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