-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
One-liner conditional stub returns an unconditional stub #817
Comments
This has probably snuck in with the The reason for this behavior is the following: sinon.stub().withArgs("foo").returns("bar").withArgs("fooz").returns("ball") This is a great illustration of the pitfalls of highly chainable APIs, and in this case,Sinon has unfortunately opted to support the wrong case IMO. Anyway, as I said, too many people depend on this functionality at this point to allow us to just "fix" it, but we certainly should consider fixing it long term. |
I understand the tension and appreciate your candor (agreeing but also favoring stability). I think in the short-term what I'll do with my own project using Sinon.js is to wrap this API with functions that configure the narrow set of doubles I want to see, which will work around this issue. The only recommendation I have for you as a maintainer is perhaps to consider an alternative API for setting up a one-and-done stub configuration. |
Could this be addressed in a major version bump? I have many of these two-liner instances throughout my test suite that I would much prefer to make one-liners. And I'll toss in my suspicion that there are more people who would prefer |
If we can come up with a reasonable name for it ( Having both styles will be confusing for newcomers, unless we improve documentation around these features, which is probably long overdue anyway. Aside: I agree method chaining is often the cause of headaches. It's very difficult to incrementally change design, when everything is dependent on every little detail of the API being the same. Since we've gone this far down the rabbit hole, we might as well keep going ... |
Hey @cjohansen after looking at your example one more time:
I tried punching this in to see what it would do:
Anyone using a long chain like this is also likely to be surprised that the last- After looking at this example I'm struggling to imagine an example of someone relying on a long chain like this one and actually desiring this behavior. If I had to bet, I'd say this change would break a lot of tests for the better (because they're currently false positives from too-permissive of stubs) |
Wow, that is interesting. Seeing this I can't even understand what the point of |
Thanks for keeping an open mind to that @cjohansen I really appreciate it. What's the best way to proceed from here? Would you be open to a PR? |
This is somewhat related to #823. |
@searls sure, a PR would be great. The current behavior is completely broken for chains defining more than one behavior anyway. |
Cool! I'll schedule some time to look at this next week |
This chaining works sometimes: findStub = sandbox.stub()
findStub
.withArgs(...)
.returns(..)
.withArgs(...)
.returns(...) This does not works properly: findStub = sandbox.stub().withArgs(...)
.returns(..)
.withArgs(...)
.returns(...) |
@cjohansen I'm really sorry for being "that guy" but I've shifted gears away from using Sinon.js for the project I'm working on, so I won't be tackling this with a PR on my own. Feel free to close/deprioritize accordingly based on your own interests, of course. |
Thank you for getting back on this! I'll apply the help-wanted label, and see if it'll grab the attention of someone with time to take a deeper look into this. Also, thank you for your investigation into this issue! |
pay4bugz ;-) |
Whoa whoa. There is a property on 'withArgs' modified stubs called 'parent' which refers back to the root of the stub. When I do my chaining I just provide explicitly that I want to pass back up the chain. var stubby = sinon.stub().throws() Works pretty well for me. Worried your fix will be a breaking change for me :( |
@launchcg-ztonia You're depending on implementation details that are not part of the official API. This will almost certainly break at some point. |
Here, I want to assert that callback function is called or not. So how can I do it using sinon js. Please suggest.
|
@apoorva-shah: the javascript output is enough to help you. there is no property called |
It would be great if someone could summarise the result of this discussion as a failing test case added to |
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. |
Any chance this will be re-considered in a near-future release? This still appears to be an issue. |
The issue has been around for years, and we are not seeing much traction for anyone trying to fix it. There is a considerable complexity in the stub api. I've been working on a new API, that is much simpler #1586. If you're curious, you could try it out and leave feedback on that pull request. |
@mantoni 2018, still working great :) |
When a conditional stub is setup in a one-liner (such that the value that's retained by the test is whatever gets returned by
returns()
), then the stub is in fact unconditional, which is very surprising and almost definitely wrong.Expected
When I do this in a test:
I expect:
But it is actually
"bar"
.Workaround
Apparently, what every Sinon user has been doing to this point is:
Which behaves as you'd expect, but is (IMHO) needlessly verbose.
Contributor guideline notes
The text was updated successfully, but these errors were encountered: