-
-
Notifications
You must be signed in to change notification settings - Fork 803
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
Add new Callback
and Returns
overloads for setting up methods with ref
parameters
#468
Conversation
c5c12e3
to
31c7269
Compare
This completes the method group which currently has only overloads for `Action` delegates. With the new overload, it is possible to set up callbacks for methods having by-ref parameters.
31c7269
to
aa96754
Compare
This isn't strictly needed just yet since present day `Returns` al- ready works with set up methods having by-ref parameters, as it does not perform parameter list type matching as strictly as `Callback` does. However it's good to have this overload for API consistency, and it will be important in a later release, should we decide to make `Returns` as strict as `Callback` (because then, without this new method overload, you would no longer be able to set up return call- backs for methods having by-ref parameters).
Adding the new generic overload `Returns<TDelegate>(TDelegate)` has an unfortunate side effect. In some cases, the compiler will pick the new overload over the preexisting `Returns(TResult)`; for example when `TResult` is `object` and a call `Returns("string")` is made. Since we cannot constrain generic type parameters to `Delegate`, we need to make the new overload non-generic to help the compilers in picking the right overload. (There's one more edge case, namely when `TResult` is `Delegate`. The new overload must forward to the preexisting overload in that case.)
ec16bcd
to
ebc6bbd
Compare
Now that the new overload for `Returns` has had to become non-generic, so should the new `Callback` overload (for consistency). Also forgot to update the XML documentation comments, bring them in line again with the actual method signatures.
ebc6bbd
to
9b64788
Compare
I think it was due to this change that a previously passing test started failing after upgrading from 4.7.145 to 4.8.0:
The fix was to specify the out parameter in the call to I'm not sure if this warrants a Breaking change note on the changelog, but it would have saved me a bit of confusion when trying to figure out why my tests started failing. |
Not sure either, but regardless of whether it's a breaking change or not, it seems to me that your test code used to be written incorrectly (setting up the wrong overload), and adding the second parameter to the setup was the correct fix. Do you agree, or have I misunderstood something? |
Yes you're exactly right and I completely agree. I suppose it probably shouldn't be considered a breaking change if the test was only passing due to a bug, so feel free to ignore this. Thanks for all your contributions to moq, its awesome! |
@ajbeaven - I'll look at your scenario more closely as soon as I get a spare moment, but unless it turns out to be an unexpected corner case that I overlooked back then, I'll probably leave things as they are now for the reason you mentioned. Glad to hear that Moq continues to be useful to you! |
This PR proposes new method overloads for the
Callback
andReturns
setup method that would allow setting up callbacks for methods havingref
parameters. They do this by accepting delegates of any delegate type (including such ones havingref
parameters); the present overloads only acceptAction
s orFunc
s, respectively.This would resolve #105.