Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spying on *etter methods #1205

Merged
merged 1 commit into from
Dec 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
}

if (types) {
// A new descriptor is needed here because we can only wrap functions
// By passing the original descriptor we would end up trying to spy non-function properties
var descriptor = {};
var methodDesc = sinon.getPropertyDescriptor(object, property);

for (var i = 0; i < types.length; i++) {
methodDesc[types[i]] = spy.create(methodDesc[types[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]));
Expand Down
28 changes: 28 additions & 0 deletions test/spy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,34 @@

assert.equals(spy.length, 3);
}
},

"getters and setters": {
"it is possible to spy getters": function () {
var object = {
get myProperty() {
return "fb41a645-24fb-4580-b4c0-17d71ecc1c74";
}
};

var descriptor = sinon.spy(object, "myProperty", ["get"]);
var value = object.myProperty; // eslint-disable-line no-unused-vars

assert.equals(descriptor.get.callCount, 1);
}
},

"it is possible to spy setters": function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is off here. This should have the same indentation as the test above it and the preceding closing brace should be moved below this test.

var object = { // eslint-disable-line accessor-pairs
set myProperty(val) {
this.otherProperty = val * 2;
}
};

var descriptor = sinon.spy(object, "myProperty", ["set"]);
object.myProperty = 10; // eslint-disable-line no-unused-vars

assert.equals(descriptor.set.callCount, 1);
}
});
}(this));
41 changes: 41 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,47 @@

assert.equals(stub.length, 3);
}
},

"getters and setters": {
"it is possible to stub getters": function () {
var object = {
get myProperty() {
return "4fb82903-6222-45e4-bfeb-a536bfc9734a";
}
};

function fakeGet() {
return "faked myProperty";
}

var descriptor = sinon.stub(object, "myProperty", {
get: fakeGet
});

assert.equals(object.myProperty, "faked myProperty");
assert.equals(descriptor.get.callCount, 1);
},

"it is possible to stub setters": function () {
var object = { // eslint-disable-line accessor-pairs
set myProperty(val) {
this.otherProperty = val * 2;
}
};

function fakeSet() {
this.otherProperty = "faked myProperty";
}

var descriptor = sinon.stub(object, "myProperty", { // eslint-disable-line accessor-pairs
set: fakeSet
});

object.myProperty = 5;
assert.equals(object.otherProperty, "faked myProperty");
assert.equals(descriptor.set.callCount, 1);
}
}
});
}(this));