diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 08c13be4..3f9bda23 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -677,6 +677,35 @@ describe("Env integration", function() { env.execute(); }); + it('allows to respy functions', function () { + + var env = new j$.Env(), + foo = { + 'bar': function () { + return 1; + } + }; + + env.allowRespy(true); + + env.describe('test suite', function(){ + env.it('spec 0', function(){ + env.spyOn(foo,'bar'); + + var error = null; + + try { + env.spyOn(foo, 'bar'); // no exception thrown + }catch(e){ + error = e; + } + expect(error).toBeNull(); + }); + }); + + env.execute(); + }); + it('removes all spies added in a spec after the spec is complete', function(done) { var env = new j$.Env(), originalFoo = function() {}, diff --git a/src/core/Env.js b/src/core/Env.js index b70b0a06..35905a25 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -270,6 +270,10 @@ getJasmineRequireObj().Env = function(j$) { return runnableResources[currentRunnable().id].spies; }}); + this.allowRespy = function( allow ){ + spyRegistry.setAllowRespy( allow ); + }; + this.spyOn = function() { return spyRegistry.spyOn.apply(spyRegistry, arguments); }; diff --git a/src/core/SpyRegistry.js b/src/core/SpyRegistry.js index 43f9edfe..53ded37e 100644 --- a/src/core/SpyRegistry.js +++ b/src/core/SpyRegistry.js @@ -4,6 +4,10 @@ getJasmineRequireObj().SpyRegistry = function(j$) { options = options || {}; var currentSpies = options.currentSpies || function() { return []; }; + this.setAllowRespy = function( allow ){ + this.respy = allow; + }; + this.spyOn = function(obj, methodName) { if (j$.util.isUndefined(obj)) { throw new Error('spyOn could not find an object to spy upon for ' + methodName + '()'); @@ -17,9 +21,12 @@ getJasmineRequireObj().SpyRegistry = function(j$) { throw new Error(methodName + '() method does not exist'); } - if (obj[methodName] && j$.isSpy(obj[methodName])) { - //TODO?: should this return the current spy? Downside: may cause user confusion about spy state - throw new Error(methodName + ' has already been spied upon'); + if (obj[methodName] && j$.isSpy(obj[methodName]) ) { + if ( !!this.respy ){ + return obj[methodName]; + }else { + throw new Error(methodName + ' has already been spied upon'); + } } var spy = j$.createSpy(methodName, obj[methodName]);