Możesz użyć spy.and.returnValues (jako Jasmine 2.4).
na przykład
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();
});
});
Jest coś, na co musisz uważać, jest jeszcze jedna funkcja, returnValuebez której zaklęcie będzie podobne s, jeśli jej użyjesz, jaśmin cię nie ostrzeże.
.returnValues- te dwie funkcje są oczywiście różne, ale przekazanie wielu argumentów do.returnValuenie powoduje błędu. Nie chcę się przyznać, ile czasu zmarnowałem z powodu tej jednej postaci.