-
-
Notifications
You must be signed in to change notification settings - Fork 802
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
Allow the equivalent of It.IsAny with ref parameters in Setup
#479
Comments
Like you said, the C# language currently doesn't allow us to do this nicely, and lambda decompilation is possible (see e. g. this proof-of-concept prototype I cooked up a while ago), but will require Moq to become dependent on .NET Standard 2, and it would take some more work—I'd like to keep this open as an option for a 4.9 release or later. For now, the least we can do is to add a helper method that "disables" matching for |
@ohadschn - Quick question: Do you think a feature switch ( The reason behind this is that I've realised that a On the other hand, a feature switch, which is more coarse-grained than the method because it sits at the mock level (instead of at the setup level) could easily be made to apply to all var mock = new Mock<IHaveMethodsWithRefParameters>();
mock.Switches |= Switch.DoNotMatchRefArguments;
...
mock.Verify(m => m.Method(1, ref dummy, 2), ...); (I know it's not quite as neat as the method, but it would be a more complete solution. And while we could of course have both, I'd prefer to introduce as few additions to the public API as possible.) Thoughts? |
How about something like: mock.NoRefArgMatching().Verify(...) Where |
That would be a nice solution. Unfortunately, there's the question of how that method call would know when the switch needs to be reset. Also, setting up a temporary context where a "shared" switch is modified means that this method wouldn't be thread-safe / reentrant. 🙁 (Personally, I don't think that |
I agree about managing this state, I was actually thinking about creating a new Mock with the same underlying object.. would that be possible? |
In theory, perhaps yes. In practice, I'd say this approach isn't worth the required effort. One would create a new |
How about something like mock.Verify(m => NoRefVerify(m.Method(1, ref dummy, 2)), ...); Where |
Your last suggestion just gave me an idea. It never occurred to me until now that we could simply do this: mock.Setup(m => m.Method(…, ref It.Ref<int>.IsAny, …));
// ^^^^^^^^^^^^^^^^^^^^^ The generic parameter obviously isn't in the usual place —it would ideally follow static partial class It
{
public static class Ref<T>
{
public static T IsAny;
}
} (Credits go to @michal-ciechan, the above technique is inspired by what he did in #343.) |
Nice! Maybe |
There's already the |
Well I suppose you could do |
All done. As a small bonus, "any" matching for mock.Protected().Setup("ProtectedMethod", …, ItExpr.Ref<int>.IsAny, …);
Just to answer this, I opted to stay with I chose
|
@ohadschn - I just published a pre-release version 4.8.0-rc1 on NuGet. It has all the new |
Hi, I have a problem with a method that has a ref parameter of type Array: But in any case during the test execution, the ref parameter is always null... EDIT: Solved
|
Sorry, accidentally pressed button. |
Following #105, it would be nice to be able to match any value of a passed by ref variable in
Setup
.Per dotnet/csharplang#158 that can't currently be achieved using
ref returns
(e.g.ref T It.IsAnyRef<T>
) with the current overloads that take expression trees.However, it might be doable by accepting a delegate and decompiling it into an expression tree at runtime. That would involve quite some work though, and for something this specific perhaps the following would suffice:
The text was updated successfully, but these errors were encountered: