From 2a8532b6f45d2d7b59f51e2e71bfff69716d1ace Mon Sep 17 00:00:00 2001 From: takasmiley Date: Tue, 18 Jul 2017 17:41:58 +0900 Subject: [PATCH 1/4] Fix #1487: incorrect withArgs().returnValue --- lib/sinon/spy.js | 3 +++ test/stub-test.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index 01a2f12da..497dc3068 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -220,6 +220,9 @@ var spyApi = { // Make return value and exception available in the calls: createCallProperties.call(this); + matchings.forEach(function (matching) { + createCallProperties.call(matching); + }); if (exception !== undefined) { throw exception; diff --git a/test/stub-test.js b/test/stub-test.js index 19579aebc..69fb032c8 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -108,6 +108,40 @@ describe("stub", function () { refute.isNull(stub.withArgs(1).firstCall); }); + describe("should work firstCall and lastCall", function () { + var testComponentA = function () { return { render: function () { return "test a"; } }; }; + var testComponentB = function () { return { render: function () { return "test b"; } }; }; + var inject; + + beforeEach(function () { + var fakeComponent = function (variant) { + return createStub().returns({ + render: createStub().returns("fake component " + variant) + }); + }; + inject = createStub().throws("Nothing set"); + inject.withArgs(testComponentA).returns(fakeComponent("a")); + inject.withArgs(testComponentB).returns(fakeComponent("b")); + }); + + it("returnValues", function () { + var config = { option: "a" }; + var component = inject(testComponentA)(config); + + assert.isTrue(inject.calledWith(testComponentA)); + assert.isFalse(inject.calledWith(testComponentB)); + + assert.isFunction(component.render); + assert.equals(component.render(), "fake component a"); + + assert.isTrue(inject.withArgs(testComponentA).returnValues[0].calledWith(config)); + assert.isTrue(inject.withArgs(testComponentA).getCall(0).returnValue.calledWith(config)); + + assert.isTrue(inject.withArgs(testComponentA).firstCall.returnValue.calledWith(config)); + assert.isTrue(inject.withArgs(testComponentA).lastCall.returnValue.calledWith(config)); + }); + }); + describe(".returns", function () { it("returns specified value", function () { var stub = createStub.create(); From 79f27e3323cebcf0e9c56706596874018b16103d Mon Sep 17 00:00:00 2001 From: takasmiley Date: Tue, 18 Jul 2017 18:03:07 +0900 Subject: [PATCH 2/4] Rename variable name --- test/stub-test.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/stub-test.js b/test/stub-test.js index 69fb032c8..0dcbb2d22 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -111,7 +111,7 @@ describe("stub", function () { describe("should work firstCall and lastCall", function () { var testComponentA = function () { return { render: function () { return "test a"; } }; }; var testComponentB = function () { return { render: function () { return "test b"; } }; }; - var inject; + var stub; beforeEach(function () { var fakeComponent = function (variant) { @@ -119,26 +119,26 @@ describe("stub", function () { render: createStub().returns("fake component " + variant) }); }; - inject = createStub().throws("Nothing set"); - inject.withArgs(testComponentA).returns(fakeComponent("a")); - inject.withArgs(testComponentB).returns(fakeComponent("b")); + stub = createStub().throws("Nothing set"); + stub.withArgs(testComponentA).returns(fakeComponent("a")); + stub.withArgs(testComponentB).returns(fakeComponent("b")); }); it("returnValues", function () { var config = { option: "a" }; - var component = inject(testComponentA)(config); + var component = stub(testComponentA)(config); - assert.isTrue(inject.calledWith(testComponentA)); - assert.isFalse(inject.calledWith(testComponentB)); + assert.isTrue(stub.calledWith(testComponentA)); + assert.isFalse(stub.calledWith(testComponentB)); assert.isFunction(component.render); assert.equals(component.render(), "fake component a"); - assert.isTrue(inject.withArgs(testComponentA).returnValues[0].calledWith(config)); - assert.isTrue(inject.withArgs(testComponentA).getCall(0).returnValue.calledWith(config)); + assert.isTrue(stub.withArgs(testComponentA).returnValues[0].calledWith(config)); + assert.isTrue(stub.withArgs(testComponentA).getCall(0).returnValue.calledWith(config)); - assert.isTrue(inject.withArgs(testComponentA).firstCall.returnValue.calledWith(config)); - assert.isTrue(inject.withArgs(testComponentA).lastCall.returnValue.calledWith(config)); + assert.isTrue(stub.withArgs(testComponentA).firstCall.returnValue.calledWith(config)); + assert.isTrue(stub.withArgs(testComponentA).lastCall.returnValue.calledWith(config)); }); }); From aa59517bd8855c0d82f072710ab28fb9c69f8a49 Mon Sep 17 00:00:00 2001 From: takasmiley Date: Tue, 18 Jul 2017 18:05:17 +0900 Subject: [PATCH 3/4] Use property of this, not variable --- test/stub-test.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/stub-test.js b/test/stub-test.js index 0dcbb2d22..566e36267 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -111,7 +111,6 @@ describe("stub", function () { describe("should work firstCall and lastCall", function () { var testComponentA = function () { return { render: function () { return "test a"; } }; }; var testComponentB = function () { return { render: function () { return "test b"; } }; }; - var stub; beforeEach(function () { var fakeComponent = function (variant) { @@ -119,26 +118,26 @@ describe("stub", function () { render: createStub().returns("fake component " + variant) }); }; - stub = createStub().throws("Nothing set"); - stub.withArgs(testComponentA).returns(fakeComponent("a")); - stub.withArgs(testComponentB).returns(fakeComponent("b")); + this.stub = createStub().throws("Nothing set"); + this.stub.withArgs(testComponentA).returns(fakeComponent("a")); + this.stub.withArgs(testComponentB).returns(fakeComponent("b")); }); it("returnValues", function () { var config = { option: "a" }; - var component = stub(testComponentA)(config); + var component = this.stub(testComponentA)(config); - assert.isTrue(stub.calledWith(testComponentA)); - assert.isFalse(stub.calledWith(testComponentB)); + assert.isTrue(this.stub.calledWith(testComponentA)); + assert.isFalse(this.stub.calledWith(testComponentB)); assert.isFunction(component.render); assert.equals(component.render(), "fake component a"); - assert.isTrue(stub.withArgs(testComponentA).returnValues[0].calledWith(config)); - assert.isTrue(stub.withArgs(testComponentA).getCall(0).returnValue.calledWith(config)); + assert.isTrue(this.stub.withArgs(testComponentA).returnValues[0].calledWith(config)); + assert.isTrue(this.stub.withArgs(testComponentA).getCall(0).returnValue.calledWith(config)); - assert.isTrue(stub.withArgs(testComponentA).firstCall.returnValue.calledWith(config)); - assert.isTrue(stub.withArgs(testComponentA).lastCall.returnValue.calledWith(config)); + assert.isTrue(this.stub.withArgs(testComponentA).firstCall.returnValue.calledWith(config)); + assert.isTrue(this.stub.withArgs(testComponentA).lastCall.returnValue.calledWith(config)); }); }); From b7ce6dd4191de80bdfd68e8bf412c9ca5ec77a5c Mon Sep 17 00:00:00 2001 From: Chris Breiding Date: Thu, 27 Jul 2017 15:36:00 -0400 Subject: [PATCH 4/4] move and write reduced test case for #1487 --- test/issues/issues-test.js | 17 +++++++++++++++++ test/stub-test.js | 33 --------------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index d99ae2b29..7e3c8c653 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -2,6 +2,7 @@ var referee = require("referee"); var sinon = require("../../lib/sinon"); +var createStub = require("../../lib/sinon/stub"); var assert = referee.assert; var refute = referee.refute; @@ -298,4 +299,20 @@ describe("issues", function () { assert.equals(window.innerHeight, 111); }); }); + + describe("#1487 - withArgs() returnValue", function () { + beforeEach(function () { + this.stub = createStub().throws("Nothing set"); + this.stub.withArgs("arg").returns("return value"); + this.stub("arg"); + }); + + it("sets correct firstCall.returnValue", function () { + assert.equals(this.stub.withArgs("arg").firstCall.returnValue, "return value"); + }); + + it("sets correct lastCall.returnValue", function () { + assert.equals(this.stub.withArgs("arg").lastCall.returnValue, "return value"); + }); + }); }); diff --git a/test/stub-test.js b/test/stub-test.js index 566e36267..19579aebc 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -108,39 +108,6 @@ describe("stub", function () { refute.isNull(stub.withArgs(1).firstCall); }); - describe("should work firstCall and lastCall", function () { - var testComponentA = function () { return { render: function () { return "test a"; } }; }; - var testComponentB = function () { return { render: function () { return "test b"; } }; }; - - beforeEach(function () { - var fakeComponent = function (variant) { - return createStub().returns({ - render: createStub().returns("fake component " + variant) - }); - }; - this.stub = createStub().throws("Nothing set"); - this.stub.withArgs(testComponentA).returns(fakeComponent("a")); - this.stub.withArgs(testComponentB).returns(fakeComponent("b")); - }); - - it("returnValues", function () { - var config = { option: "a" }; - var component = this.stub(testComponentA)(config); - - assert.isTrue(this.stub.calledWith(testComponentA)); - assert.isFalse(this.stub.calledWith(testComponentB)); - - assert.isFunction(component.render); - assert.equals(component.render(), "fake component a"); - - assert.isTrue(this.stub.withArgs(testComponentA).returnValues[0].calledWith(config)); - assert.isTrue(this.stub.withArgs(testComponentA).getCall(0).returnValue.calledWith(config)); - - assert.isTrue(this.stub.withArgs(testComponentA).firstCall.returnValue.calledWith(config)); - assert.isTrue(this.stub.withArgs(testComponentA).lastCall.returnValue.calledWith(config)); - }); - }); - describe(".returns", function () { it("returns specified value", function () { var stub = createStub.create();