本文介紹了如何為 Jasmine 間諜的多個調用提供不同的返回值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
假設我在監視這樣的方法:
Say I'm spying on a method like this:
spyOn(util, "foo").andReturn(true);
被測函數多次調用util.foo
.
是否可以讓間諜在第一次調用時返回 true
,但第二次返回 false
?還是有其他方法可以解決這個問題?
Is it possible to have the spy return true
the first time it's called, but return false
the second time? Or is there a different way to go about this?
推薦答案
你可以使用spy.and.returnValues(如 Jasmine 2.4).
You can use spy.and.returnValues (as Jasmine 2.4).
例如
describe("A spy, when configured to fake a series of return values", function() {
beforeEach(function() {
spyOn(util, "foo").and.returnValues(true, false);
});
it("when called multiple times returns the requested values in order", function() {
expect(util.foo()).toBeTruthy();
expect(util.foo()).toBeFalsy();
expect(util.foo()).toBeUndefined();
});
});
有一點你必須注意,還有一個函數會類似地拼寫returnValue
而沒有s
,如果你使用它,jasmine不會警告你.
There is some thing you must be careful about, there is another function will similar spell returnValue
without s
, if you use that, jasmine will not warn you.
這篇關于如何為 Jasmine 間諜的多個調用提供不同的返回值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!