Added returnValues functionality to spy strategy

This commit is contained in:
Mridul
2014-09-13 12:41:20 +10:00
parent e8178d061b
commit 709e032d1c
2 changed files with 23 additions and 2 deletions

View File

@@ -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});

View File

@@ -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;