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

reset() does not reset a stub #806

Closed
dotchev opened this issue Aug 15, 2015 · 8 comments
Closed

reset() does not reset a stub #806

dotchev opened this issue Aug 15, 2015 · 8 comments

Comments

@dotchev
Copy link
Contributor

dotchev commented Aug 15, 2015

reset resets only a spy but not a stub

Consider this code:

var sinon = require('sinon');
var stub = sinon.stub();
stub.withArgs(1).returns('Bye');
stub.reset();
stub.returns('Hello');
console.log('stub(1)', stub(1));

it prints

stub(1) Bye

if I change stub.reset(); to stub.resetBehavior(); it works as expected, i.e. prints stub(1) Hello.
But resetBehavior is not documented (#267) and it would be more consistent and natural if reset works for both spies and stubs.

Another option is to change stub.reset(); to stub = sinon.stub();.
But It is not always feasible to create a new stub in each test. Consider this example:

var stub = spy.stub();
var obj = createExpensiveObject(stub);

it('test1', function() {
  // setup stub in some way
  obj.test1();
});

it('test2', function() {
  // setup stub in a different way
  obj.test2();
});
@mroderick
Copy link
Member

This ticket is a bit convoluted. Are you reporting a bug or requesting a change?

Stubs are intentionally cheap to create, so if you want fresh behaviour, just create another one?

@dotchev
Copy link
Contributor Author

dotchev commented Aug 20, 2015

I would consider it a change request, which is

If reset() is called on a stub, it should reset it to an initial state

One could consider it also a design bug because a stub (being also a spy) has a reset() function but it only resets only the spy part of it.

While stubs are cheap to create, the objects where one needs to inject them might not be so cheap. So just creating a new stub might not be enough if need also to recreate the object where you inject the new stub.
See my example above. I need to use a different stub in each test (which is usually the case). To do this now, I have to call createExpensiveObject for each test, just to pass it the new stub.

dotchev added a commit to dotchev/Sinon.JS that referenced this issue Sep 14, 2015
@fearphage
Copy link
Member

Did this just take away a feature? Before you could have acheived this by calling:

stub.reset();
stub.resetBehvaior();

Now you can only call .resetBehvaior() alone. You cannot reset the call counts without resetting the behavior. This seems like a regression.

@mroderick
Copy link
Member

Now you can only call .resetBehvaior() alone. You cannot reset the call counts without resetting the behavior. This seems like a regression.

That's a good point.

Perhaps it would be better if we expanded the api a bit.

// resets both call history and behaviour
stub.reset();

// reset call history
stub.resetHistory();

// reset behaviour
stub.resetBehavior()

@dotchev
Copy link
Contributor Author

dotchev commented Sep 30, 2015

Seems reasonable

@fearphage
Copy link
Member

@mroderick That seems solid to me.

@mroderick mroderick mentioned this issue Oct 1, 2015
@mroderick
Copy link
Member

So that the idea is not lost, I've created issue #863 for adding .resetHistory to the api.

@fearphage
Copy link
Member

This change should be called out in the release notes as a breaking change. This is a change to the expected behavior of .reset().

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

3 participants