-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Inconsistent return value of stubs() / expects() #524
Comments
@vlad-pisanov Thanks for opening this issue. You are correct that the behaviour does not match the documentation and that seems to have been the case since the feature was first introduced 13 years ago!! I think fixing it should be relatively straightforward, but I am slightly questioning whether the documented behaviour is actually sensible. Since I don't think the order of entries in a |
@floehopper A list of expectations might be nice, but arguably overkill. I like how the docs say that:
If it's exactly equivalent, then the result would be the last Expectation defined. As of Ruby 1.9.1, Hash keys enumerate in the order they were inserted so it should be predictable. My use case is actually simple, in our codebase, we use this shortcut quite a bit: UserLogic.expects(age: 42).twice ^ if this syntax worked even with a single key on |
Thanks - it's helpful to have that context! What you say makes a lot of sense to me.
I thought hash keys being ordered according to insertion order wasn't part of the Ruby "spec" and thus not guaranteed for all flavours of Ruby, but I might be out-of-date on that understanding. The most authoritative information I've been able to find after a quick internet search is this Stack Overflow answer, but that's very old and inconclusive. However, I note that the following spec is in the current Ruby repo, so maybe it's OK to rely on after all: |
Even when passed a hash of method names vs return values. This is the same behaviour as for ObjectMethods#expects & #stubs and the behaviour described by the documentation for Mock#expects & #stubs. Fixes #524.
Even when passed a hash of method names vs return values. This is the same behaviour as for ObjectMethods#expects & #stubs and the behaviour described by the documentation for Mock#expects & #stubs. Fixes #524.
@vlad-pisanov I've just merged a fix (#525). Is there any chance you could try out the latest version in |
@floehopper works like a charm! Thank you 🙏 |
@vlad-pisanov Thanks for testing it. We'll try to get a release out as soon as we can. |
I've made this a minor version bump, because technically it does change some published behaviour very slightly (#524) albeit to bring it *into* line with the existing documentation!
@vlad-pisanov This was released in v1.14.0. |
According to the docs, the result of
stubs()
andexpects()
is the "last-built expectation which can be further modified by methods on Expectation. This applies to both.stubs(:foo)
and.stubs(foo: 1)
invocations.This seems to be true when stubbing methods on any class except
Mock
objects. ForMock
objects, the return value is sometimes anExpectation
, and sometimes aHash
.The text was updated successfully, but these errors were encountered: