From 13bf81d7ad2cd1f792c636ebf910bd53e2081304 Mon Sep 17 00:00:00 2001 From: Phred Date: Mon, 9 Nov 2015 08:53:03 -0600 Subject: [PATCH 1/4] updated sinon.spy() to properly handle getters and setters --- lib/sinon/spy.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index b561ff579..7faa2eeaa 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -18,6 +18,8 @@ var slice = Array.prototype.slice; var callId = 0; function spy(object, property, types) { + var descriptor, i, methodDesc; + if (!property && typeof object === "function") { return spy.create(object); } @@ -27,11 +29,13 @@ function spy(object, property, types) { } if (types) { - var methodDesc = sinon.getPropertyDescriptor(object, property); - for (var i = 0; i < types.length; i++) { - methodDesc[types[i]] = spy.create(methodDesc[types[i]]); + descriptor = {}; + methodDesc = sinon.getPropertyDescriptor(object, property); + + for (i = 0; i < types.length; i++) { + descriptor[types[i]] = spy.create(methodDesc[types[i]]); } - return sinon.wrapMethod(object, property, methodDesc); + return sinon.wrapMethod(object, property, descriptor); } return sinon.wrapMethod(object, property, spy.create(object[property])); From 1e912b4b719ca2c4dd356c3de56e96fef68870b7 Mon Sep 17 00:00:00 2001 From: Phred Date: Mon, 9 Nov 2015 09:05:10 -0600 Subject: [PATCH 2/4] added spy getter/setter tests --- test/spy-test.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/spy-test.js b/test/spy-test.js index 954d78257..77ce7feda 100644 --- a/test/spy-test.js +++ b/test/spy-test.js @@ -231,6 +231,55 @@ assert.isArray(spy.exceptions); }, + "works with getters": function () { + var object = { + get property() { + return 42; + } + }; + var spy = sinon.spy(object, "property", ["get"]); + + assert.equals(object.property, 42); + assert(spy.get.calledOnce); + }, + + "works with setters": function () { + var object = { + get test() { + return this.property; + }, + set test(value) { + this.property = value * 2; + } + }; + var spy = sinon.spy(object, "test", ["set"]); + + object.test = 42; + assert(spy.set.calledOnce); + assert(spy.set.calledWith(42)); + + assert.equals(object.test, 42); + assert.equals(object.property, 84); + }, + + "works with setters and getters combined": function () { + var object = { + get test() { + return this.property; + }, + set test(value) { + this.property = value; + } + }; + var spy = sinon.spy(object, "test", ["get", "set"]); + + object.test = 42; + assert(spy.set.calledOnce); + + assert.equals(object.test, 42); + assert(spy.get.calledOnce); + }, + ".named": { "sets displayName": function () { var spy = sinon.spy(); From 63efa9ede712bb57567b0e5a53e7257e895ecf4c Mon Sep 17 00:00:00 2001 From: Phred Date: Mon, 9 Nov 2015 09:06:06 -0600 Subject: [PATCH 3/4] added missing test (pushes spy coverage to 100%) --- test/call-test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/call-test.js b/test/call-test.js index a9b4909ba..e60d120c5 100644 --- a/test/call-test.js +++ b/test/call-test.js @@ -1268,6 +1268,12 @@ spy.reset(); spy.call(true); assert.equals(spy.printf("%t"), "true"); + }, + + unmatched: function () { + var spy = sinon.spy(); + + assert.equals(spy.printf("%λ"), "%λ"); } } }); From 4dea77cc3ad7dc14d7d3b404ee075c15732ed33e Mon Sep 17 00:00:00 2001 From: Phred Date: Mon, 9 Nov 2015 09:22:21 -0600 Subject: [PATCH 4/4] fixed spy tests --- test/spy-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/spy-test.js b/test/spy-test.js index 77ce7feda..c64501366 100644 --- a/test/spy-test.js +++ b/test/spy-test.js @@ -258,7 +258,7 @@ assert(spy.set.calledOnce); assert(spy.set.calledWith(42)); - assert.equals(object.test, 42); + assert.equals(object.test, 84); assert.equals(object.property, 84); }, @@ -268,7 +268,7 @@ return this.property; }, set test(value) { - this.property = value; + this.property = value * 2; } }; var spy = sinon.spy(object, "test", ["get", "set"]); @@ -276,7 +276,7 @@ object.test = 42; assert(spy.set.calledOnce); - assert.equals(object.test, 42); + assert.equals(object.test, 84); assert(spy.get.calledOnce); },