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

createStubInstance - optional overrides #1857

Closed
ifrost opened this issue Jul 10, 2018 · 3 comments
Closed

createStubInstance - optional overrides #1857

ifrost opened this issue Jul 10, 2018 · 3 comments

Comments

@ifrost
Copy link
Contributor

ifrost commented Jul 10, 2018

Is your feature request related to a problem? Please describe.
Stubbing multiple properties require to write the var name multiple times, for example:

var stubbedObject = sinon.createStubInstance(MyClass);
stubbedObject.foo = sinon.stub().returns(2);
stubbedObject.bar = sinon.stub().callsFake(function() { return 1; });
stubbedObject.xyzzy = sinon.stub().returns('test');

Describe the solution you'd like
It'd be nice to have a shortcut syntax to provide optional overrides for createStubInstance, for example:

var stubbedObject = sinon.createStubInstance(MyClass, {
  foo: sinon.stub().returns(2),
  bar: sinon.stub().callsFake(function() { return 1; }),
  xyzzy: sinon.stub().returns('test')
});

Describe alternatives you've considered
The same result could be achieved with "with":

with (stubbedObject = sinon.createStubInstance(MyClass)) {
  foo = sinon.stub().returns(2);
  bar = sinon.stub().callsFake(function() { return 1; });
  xyzzy = sinon.stub().returns('test');
}

"with" expression, however, is marked as deprecated and probably it's not the best idea to use it.

Another alternative I thought about was to provide functions to give more flexibility:

var stubbedObject = sinon.createStubInstance(MyClass, {
  foo: function(stub, property) { 
    stub[property] = sinon.stub().returns(2);
  }
});

The version above would be easier to reuse. For example, sometimes we use fluent interfaces:

var stubbedObject = sinon.createStubInstance(MyClass);
stubbedObject.foo = sinon.stub().returnsThis();
stubbedObject.bar = sinon.stub().returnsThis();
stubbedObject.xyzzy = sinon.stub().returnsThis();

Could have even shorter syntax:

var returnsThis = function(stub, propertyName) {
  stub[propertyName] = sinon.stub().returnsThis();
}

var stubbedObject = sinon.createStubInstance(MyClass, {
  foo: returnsThis,
  bar: returnsThis,
  xyzzy: returnsThis
});
@stale
Copy link

stale bot commented Sep 8, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 8, 2018
@ifrost
Copy link
Contributor Author

ifrost commented Sep 12, 2018

Hi, the PR for it is still open. Any chance of getting it merged?

@stale stale bot removed the stale label Sep 12, 2018
@mroderick
Copy link
Member

This was implemented in #1864 and published to NPM as sinon@6.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants