From fbcdbf5ab1b062b18f1f3138dde0d2aa87902d5e Mon Sep 17 00:00:00 2001 From: Fangzhou Li Date: Sun, 29 Apr 2018 06:02:43 +0800 Subject: [PATCH] Allow omitting the name argument: `createSpy(func)` --- spec/core/SpySpec.js | 7 +++++++ src/core/Env.js | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/spec/core/SpySpec.js b/spec/core/SpySpec.js index fb90657f..55adb7c7 100644 --- a/spec/core/SpySpec.js +++ b/spec/core/SpySpec.js @@ -20,6 +20,13 @@ describe('Spies', function () { expect(spy.bob).toEqual("test"); }); + it("should allow you to omit the name argument and only pass the originalFn argument", function() { + var fn = function test() {}; + var spy = env.createSpy(fn); + + expect(spy.and.identity).toEqual("test"); + }) + it("warns the user that we intend to overwrite an existing property", function() { TestClass.prototype.someFunction.and = "turkey"; diff --git a/src/core/Env.js b/src/core/Env.js index 74b31829..c351f7b9 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -474,6 +474,11 @@ getJasmineRequireObj().Env = function(j$) { }; this.createSpy = function(name, originalFn) { + if (arguments.length === 1 && j$.isFunction_(name)) { + originalFn = name; + name = originalFn.name; + } + return spyFactory.createSpy(name, originalFn); };