说我侦察一个这样的方法:
spyOn(util, "foo").andReturn(true);
被测函数多次调用util.foo。
是否有可能让spy在第一次调用时返回true,但是第二次返回false?或者有不同的方法去做这个?
你可以使用spy.and.returnValues(作为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,如果你使用它,茉莉花不会警告你。
http://stackoverflow.com/questions/26898613/how-to-have-different-return-values-for-multiple-calls-on-a-jasmine-spy
本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:javascript – 如何在Jasmine间谍的多个调用有不同的返回值