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
sandbox.restore() restores to the wrong value when sandbox.stub()ing twice. #1201
Comments
Hi, while it's great to have this fixed, I'm not sure we should support this use case. Multiple stubbings of the same property seems like an anti-pattern IMHO, as it leads to hard to read/complex tests. So I'd rather have Sinon throw an explicit error on encountering this than trying to fix it, but ... there's more than one way to view things, so I'd be interested to hear what the others say before flat out dismissing this work to improve this. |
I would also prefer that |
From briefly looking through the code, it looks like this would be a somewhat involved refactor, since the existing wrap for properties just overwrites the property and has a closured method to restore the property; whereas for methods, sinon stores extra data on the function itself (which we are afforded by Functions being non-primitives)... however, the bookkeeping for properties would have to be in an auxiliary data structure... presumably in a map from object to set of stubbed property names. For being the most defensive, it would make sense for this data structure to be shared across all sandboxes, otherwise, you may re-define a property multiple times. Does this sound correct? However, I would argue that even if the original suggestion can encourage an anti-pattern, it does seem more intuitive for the restore to happen in reverse, as you are effectively "rewinding" the changes that were made by sinon. In my mind, it makes sense that the the core logic is applying these changes to stub things, applying the changes in reverse to restore things, and then erroring is to discourage the anti-patterns of stubbing things twice. I propose that suggested change should still be added (even easily added for the 1.x release), and then bookkeeping can be added on-top of it in a separate PR. |
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. |
How to reproduce
http://jsfiddle.net/totfnww7/1/
What did you expect to happen?
Expected last output to be: "After restore: 1, correct: true" -- expecting sinon to restore to the original value
What actually happens
But it outputted "After restore: 2, correct: false" -- sinon restored to the //previous// stubbed value.
Actual cause
A fix that I think works is:
I can submit a PR if this sounds correct.
The text was updated successfully, but these errors were encountered: