From d1478927bf86f3797b8bc5a44ebbff88a403d4e5 Mon Sep 17 00:00:00 2001 From: edeustace Date: Sun, 21 Jun 2015 23:16:07 +0100 Subject: [PATCH 1/2] add callsFake --- lib/sinon/behavior.js | 12 ++++++++++-- test/stub-test.js | 14 +++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/sinon/behavior.js b/lib/sinon/behavior.js index 0f6e3864c..4fce7b3fb 100644 --- a/lib/sinon/behavior.js +++ b/lib/sinon/behavior.js @@ -137,7 +137,12 @@ }, invoke: function invoke(context, args) { - callCallback(this, args); + + if (this.callFakeFn) { + this.callFakeFn.apply(null, args); + } else { + callCallback(this, args); + } if (this.exception) { throw this.exception; @@ -170,7 +175,10 @@ throw new Error("Defining a stub by invoking \"stub.onCall(...).withArgs(...)\" is not supported. " + "Use \"stub.withArgs(...).onCall(...)\" to define sequential behavior for calls with certain arguments."); }, - + callsFake: function callsFake(fn) { + this.callFakeFn = fn; + return this; + }, callsArg: function callsArg(pos) { if (typeof pos != "number") { throw new TypeError("argument index is not number"); diff --git a/test/stub-test.js b/test/stub-test.js index debffa97c..e3e7d8605 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -215,7 +215,19 @@ } } }, - + ".callsFake" : { + + setUp: function () { + this.stub = sinon.stub.create(); + }, + + "calls fake function" : function () { + var callback = sinon.stub.create(); + this.stub.callsFake(callback); + this.stub(1, 2, callback); + assert(callback.called); + } + }, ".callsArg": { setUp: function () { this.stub = sinon.stub.create(); From 830aa74330d714397386d2f986077cb121beab58 Mon Sep 17 00:00:00 2001 From: edeustace Date: Sun, 21 Jun 2015 23:21:28 +0100 Subject: [PATCH 2/2] rename callback -> fake in test --- test/stub-test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/stub-test.js b/test/stub-test.js index e3e7d8605..29aa7a271 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -222,10 +222,10 @@ }, "calls fake function" : function () { - var callback = sinon.stub.create(); - this.stub.callsFake(callback); - this.stub(1, 2, callback); - assert(callback.called); + var fake = sinon.stub.create(); + this.stub.callsFake(fake); + this.stub(1, 2); + assert(fake.calledWith(1, 2)); } }, ".callsArg": {