問題描述
我有一個場景,我想在調用回調之后在 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!