-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
Returns(null) requires explicit typecast in Strict mocks #600
Comments
@Caraul: I've noticed before that Is it about this:
There's probably very little that we can do about this. Changing this would likely require changing the method overloads' definitions, which would be likely to cause breaking changes. Or is the issue about this:
If so, then please confirm and I'll take a closer look at it. |
Please forget the above question, I misunderstood. This appears to be a regression introduced in Moq 4.8.0. Before that, it appears to have been legal to call |
Looks like the problem is here in 4.8.0: which used to be (in 4.7.145): Note that the We should probably restore it, something along these lines: if (value == null)
{
this.valueDel = (Func<TResult>)(() => default(TResult));
// though I wonder why Moq used `default(TResult)` instead of `null` previously...?
}
else
{
ValidateReturnDelegate(value);
this.valueDel = value;
}
... Would you like to work on this? |
Of course yes, I'll fix it. |
Hi,
Returns(null)
doesn't compile due to ambiguous invocation.Returns((SomeType)null)
is "inferred" toReturns(SomeType value)
which has no problems.Returns<SomeType>(null)
is "inferred" toReturns(Func<SomeType> valueFunction)
which is simply set null delegate withReturnValueKind.Explicit
. ForMockBehavior.Strict
inMethodCallReturn.Execute
it throws withExceptionReason.ReturnValueRequired
(despite of setReturnValueKind.Explicit
). Is it correct? I'd suppose(this.valueDel != null)
should be replaced with(this.returnValueKind == ReturnValueKind.Explicit)
and either calls delegate orinvocation.Return(default(TResult))
for explicit null values. Or maybe shouldReturns(Func<SomeType>)
behave the same asReturns(Delegate valueFunction)
which has special treatment for nulls "because Moq has been very forgiving with incorrectReturns
in the past."?The text was updated successfully, but these errors were encountered: