本文介紹了如何為 Jasmine 間諜的多個調(diào)用提供不同的返回值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
假設(shè)我在監(jiān)視這樣的方法:
Say I'm spying on a method like this:
spyOn(util, "foo").andReturn(true);
被測函數(shù)多次調(diào)用util.foo
.
是否可以讓間諜在第一次調(diào)用時返回 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();
});
});
有一點(diǎn)你必須注意,還有一個函數(shù)會類似地拼寫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.
這篇關(guān)于如何為 Jasmine 間諜的多個調(diào)用提供不同的返回值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!