From a2ac5ef3b6064fe7b613d9548990436cffdd9419 Mon Sep 17 00:00:00 2001 From: Christopher Amavisca and Greg Cobb Date: Wed, 5 Mar 2014 14:27:19 -0800 Subject: [PATCH] Throw error if you define a spy outside of a runnable [#66789174] --- lib/jasmine-core/jasmine.js | 3 +++ spec/core/integration/EnvSpec.js | 23 +++++++++++++++++++++++ src/core/Env.js | 3 +++ 3 files changed, 29 insertions(+) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 14bd7ed8..fba0f67a 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -575,6 +575,9 @@ getJasmineRequireObj().Env = function(j$) { }; var spyRegistry = new j$.SpyRegistry({currentSpies: function() { + if(!currentRunnable()) { + throw new Error('Spies must be created in a before function or a spec'); + } return runnableResources[currentRunnable().id].spies; }}); diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 3dd0a2fc..182edd78 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -802,4 +802,27 @@ describe("Env integration", function() { env.execute(); }); + + it('throws an exception if you try to create a spy outside of a runnable', function (done) { + var env = new j$.Env(), + obj = {fn: function () {}}, + exception; + + env.describe("a suite", function () { + try { + env.spyOn(obj, 'fn'); + } catch(e) { + exception = e; + } + }); + + var assertions = function() { + expect(exception.message).toBe('Spies must be created in a before function or a spec'); + done(); + }; + + env.addReporter({jasmineDone: assertions}); + + env.execute(); + }); }); diff --git a/src/core/Env.js b/src/core/Env.js index dffe97ad..111c2519 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -201,6 +201,9 @@ getJasmineRequireObj().Env = function(j$) { }; var spyRegistry = new j$.SpyRegistry({currentSpies: function() { + if(!currentRunnable()) { + throw new Error('Spies must be created in a before function or a spec'); + } return runnableResources[currentRunnable().id].spies; }});