Add returnValues() function to SpyAnd interface (#9175)

This commit is contained in:
Thanabodee Charoenpiriyakij
2016-05-05 23:14:46 +07:00
committed by Masahiro Wakame
parent e843172c97
commit 36a1be34db
2 changed files with 36 additions and 0 deletions

View File

@@ -358,6 +358,40 @@ describe("A spy, when configured to fake a return value", function () {
});
});
describe("A spy, when configured to fake a series of return values", function() {
var foo: any, bar: any;
beforeEach(function() {
foo = {
setBar: function(value: any) {
bar = value;
},
getBar: function() {
return bar;
}
};
spyOn(foo, "getBar").and.returnValues("fetched first", "fetched second");
foo.setBar(123);
});
it("tracks that the spy was called", function() {
foo.getBar(123);
expect(foo.getBar).toHaveBeenCalled();
});
it("should not affect other functions", function() {
expect(bar).toEqual(123);
});
it("when called multiple times returns the requested values in order", function() {
expect(foo.getBar()).toEqual("fetched first");
expect(foo.getBar()).toEqual("fetched second");
expect(foo.getBar()).toBeUndefined();
});
});
describe("A spy, when configured with an alternate implementation", function () {
var foo: any, bar: any, fetchedBar: any;

View File

@@ -431,6 +431,8 @@ declare namespace jasmine {
callThrough(): Spy;
/** By chaining the spy with and.returnValue, all calls to the function will return a specific value. */
returnValue(val: any): Spy;
/** By chaining the spy with and.returnValues, all calls to the function will return specific values in order until it reaches the end of the return values list. */
returnValues(...values: any[]): Spy;
/** By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied function. */
callFake(fn: Function): Spy;
/** By chaining the spy with and.throwError, all calls to the spy will throw the specified value. */