-
-
Notifications
You must be signed in to change notification settings - Fork 772
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
Document how to stub getters and setters #1095
Comments
With the introduction of
Is this feature still supported? |
Yes, but using a different syntax. See the get and set methods at the bottom of http://sinonjs.org/releases/v2.0.0/stubs/. Closing this. |
Perfect. Thanks for the quick response! |
The new syntax is awesome. But it doesn't tell how a stubbed getter would be restored? |
@derwaldgeist Oh, but it does. As I mentioned above, just see the bottom of the page: http://sinonjs.org/releases/v2.3.1/stubs/ P.S. If you feel the docs could be improved, please feel free to push a PR. The docs are in this repo. A working example// test.js
var sinon = require('sinon');
console.log('#getter-stub example')
var obj = { 'foo' : 'foo-val' }
console.log(obj.foo);
var stub = sinon.stub(obj, 'foo').get( () => 'fake-val' );
console.log(obj.foo);
// this won't work as obj.foo returns a value, not the stub
// - might be possible to improve the API here!
// obj.foo.restore();
stub.restore();
console.log(obj.foo);
console.log('\n#value-stub example')
var stub2 = sinon.stub(obj, 'foo').value( 'new-fake-val' );
console.log(obj.foo);
// this won't work for the above reasons
// => TypeError: obj.foo.restore is not a function
// obj.foo.restore();
stub2.restore();
console.log(obj.foo); Output``` $ node test #getter-stub example foo-val fake-val foo-val#value-stub example
|
If you're reading this from a google search you want to read https://sinonjs.org/releases/latest/stubs/ instead! |
PR #692 introduced the feature of stubbing getters and setters. This is as of yet undocumented. Some of the comments show examples of how to make use of this feature.
The text was updated successfully, but these errors were encountered: