From 9fbd0ba55175b0848a3285e626e6aad457db3e29 Mon Sep 17 00:00:00 2001 From: Gregg Van Hove Date: Wed, 15 Feb 2017 15:55:27 -0800 Subject: [PATCH] Nicer error messages for `spyOn` when `null` is provided Fixes #1258 --- lib/jasmine-core/jasmine.js | 4 ++-- spec/core/SpyRegistrySpec.js | 16 ++++++++++++++++ src/core/SpyRegistry.js | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 278a4393..a9ed5975 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -3523,11 +3523,11 @@ getJasmineRequireObj().SpyRegistry = function(j$) { this.spyOn = function(obj, methodName) { - if (j$.util.isUndefined(obj)) { + if (j$.util.isUndefined(obj) || obj === null) { throw new Error(getErrorMsg('could not find an object to spy upon for ' + methodName + '()')); } - if (j$.util.isUndefined(methodName)) { + if (j$.util.isUndefined(methodName) || methodName === null) { throw new Error(getErrorMsg('No method name supplied')); } diff --git a/spec/core/SpyRegistrySpec.js b/spec/core/SpyRegistrySpec.js index df68b50f..4f09c932 100644 --- a/spec/core/SpyRegistrySpec.js +++ b/spec/core/SpyRegistrySpec.js @@ -16,6 +16,22 @@ describe("SpyRegistry", function() { }).toThrowError(/No method name supplied/); }); + it("checks that the object is not `null`", function() { + var spyRegistry = new jasmineUnderTest.SpyRegistry(); + expect(function() { + spyRegistry.spyOn(null, 'pants'); + }).toThrowError(/could not find an object/); + }); + + it("checks that the method name is not `null`", function() { + var spyRegistry = new jasmineUnderTest.SpyRegistry(), + subject = {}; + + expect(function() { + spyRegistry.spyOn(subject, null); + }).toThrowError(/No method name supplied/); + }); + it("checks for the existence of the method", function() { var spyRegistry = new jasmineUnderTest.SpyRegistry(), subject = {}; diff --git a/src/core/SpyRegistry.js b/src/core/SpyRegistry.js index ce4f1b1f..d8d4cf7f 100644 --- a/src/core/SpyRegistry.js +++ b/src/core/SpyRegistry.js @@ -12,11 +12,11 @@ getJasmineRequireObj().SpyRegistry = function(j$) { this.spyOn = function(obj, methodName) { - if (j$.util.isUndefined(obj)) { + if (j$.util.isUndefined(obj) || obj === null) { throw new Error(getErrorMsg('could not find an object to spy upon for ' + methodName + '()')); } - if (j$.util.isUndefined(methodName)) { + if (j$.util.isUndefined(methodName) || methodName === null) { throw new Error(getErrorMsg('No method name supplied')); }