-
-
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
Setup with generic args (nullable and non-nullable) always calls latest setup, not taking into account generic type argument of callee #1139
Comments
Fiddled around with it, seems the following is potentially a step in the right direction (and would severely reduce the amount of reflection code required to setup all my enums)
The only "issue" with this, is that I'm getting a TargetInvocationException and not my custom thrown Exception("Failed to parse") |
I cannot help you with your original question, but this sounds like a problem with how Moq internally invokes the callback. We have the necessary infrastructure in place to avoid this problem, it appears we've missed one or two places where it should get used. I'll look into it. |
There you go. In the upcoming Moq 4.16.1, the wrapping |
P.S. You may want to create a type matcher that only matches enum types... perhaps something along the lines of: [TypeMatcher]
readonly struct AnyEnum : ITypeMatcher
{
public bool Matches(Type type)
{
return (Nullable.GetUnderlyingType(type) ?? type).IsEnum;
}
} |
Ah cheers, didn't know such functionality existed 😃 Anyhow, while I managed to circumvent the issue I was having by using the mock from my second comment, I sure hope someone will be able to find the issue with the setups not being called correctly |
My impression is that this problem is larger than just Moq. Nullable value types can at times be confusing, even more so when generic type parameters enter the picture. The .NET compilers have certain coercion rules brtween some Because that code has been around quite long, I suspect we cannot easily change the coercion rules (if that's what would be necessary to fix your use case) without breaking a lot of user code. Let's leave this open a while longer, perhaps someone will be able to come up with alternative setups that resolve this. |
There has been no activity for over a year, so I am finally closing this issue. |
Hey,
I recently upgraded from Moq 4.10 to 4.16 and I started experiencing weird NullReferenceErrors in tests that previously worked just fine.
After some investigation, I managed to reproduce the issue with the following code:
My first call to
.DoSomething<MyTestEnum>("Two")
I would expect the first setup to be called. Instead, it is going for the second one, the one with the MyTestEnum? generic argument. As such, T in the ParseEnum is MyTestEnum? and not MyTestEnumAs my input value is valid, this doesn't pose an issue.
The issue however is when I pass an invalid value, if the argument is non-nullable, I expect an exception to be thrown. (My reasoning in this case is: when parsing a string as an non-nullable enum, the parsing must succeed. When its a nullable enum, NULL is the expected value when parsing failed)
But, because T in ParseEnum is now always MyTestEnum?, irregardless of what has been called.
When I swap the order of Setup calls, it will be the other way around. T will always be MyTestEnum
Any thoughts on how to circumvent this?
I have a bunch of enums (nullable and non-nullable) for which i'm registering setups through reflection, which now no longer work :(
The text was updated successfully, but these errors were encountered: