From 709e032d1c8ecf1068b3a7985f9eef81388ed4f8 Mon Sep 17 00:00:00 2001 From: Mridul Date: Sat, 13 Sep 2014 12:41:20 +1000 Subject: [PATCH] Added returnValues functionality to spy strategy --- spec/core/SpyStrategySpec.js | 13 +++++++++++++ src/core/SpyStrategy.js | 12 ++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js index 3fc32a37..2c9d7f14 100644 --- a/spec/core/SpyStrategySpec.js +++ b/spec/core/SpyStrategySpec.js @@ -46,6 +46,19 @@ describe("SpyStrategy", function() { expect(returnValue).toEqual(17); }); + it("can return specified values in order specified when executed", function() { + var originalFn = jasmine.createSpy("original"), + spyStrategy = new j$.SpyStrategy({fn: originalFn}); + + spyStrategy.returnValues('value1', 'value2', 'value3'); + + expect(spyStrategy.exec()).toEqual('value1'); + expect(spyStrategy.exec()).toEqual('value2'); + expect(spyStrategy.exec()).toBe('value3'); + expect(spyStrategy.exec()).toBeUndefined(); + expect(originalFn).not.toHaveBeenCalled(); + }); + it("allows an exception to be thrown when executed", function() { var originalFn = jasmine.createSpy("original"), spyStrategy = new j$.SpyStrategy({fn: originalFn}); diff --git a/src/core/SpyStrategy.js b/src/core/SpyStrategy.js index e58438ed..e280d5d5 100644 --- a/src/core/SpyStrategy.js +++ b/src/core/SpyStrategy.js @@ -1,4 +1,4 @@ -getJasmineRequireObj().SpyStrategy = function() { +getJasmineRequireObj().SpyStrategy = function () { function SpyStrategy(options) { options = options || {}; @@ -28,7 +28,15 @@ getJasmineRequireObj().SpyStrategy = function() { return getSpy(); }; - this.throwError = function(something) { + this.returnValues = function () { + var values = Array.prototype.slice.call(arguments); + plan = function () { + return values.shift(); + }; + return getSpy(); + }; + + this.throwError = function (something) { var error = (something instanceof Error) ? something : new Error(something); plan = function() { throw error;