8000 'Returns' regression with null function callback by Caraul · Pull Request #602 · devlooped/moq · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1

* Usage of `ReturnsExtensions.ThrowsAsync()` can cause `UnobservedTaskException` (@snrnats, #595)
* `ReturnsAsync` and `ThrowsAsync` with delay parameter starts timer at setup (@snrnats, #595)
* `Returns` regression with null function callback (@Caraul, #602)


## 4.8.2 (2018-02-23)
Expand Down
36 changes: 36 additions & 0 deletions Moq.Tests/ReturnsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ public void ReturnsNullValueIfNullFunc()
mock.VerifyAll();
}

[Fact]
public void ReturnsNullValueIfNullDelegate()
{
var mock = new Mock<IFoo>();
mock.Setup(foo => foo.Execute("Whatever")).Returns((Delegate)null);
Assert.Null(mock.Object.Execute("Whatever"));
mock.VerifyAll();
}

[Fact]
public void ReturnsNullValueIfSpecifiedForStrictMock()
{
var mock = new Mock<IFoo>(MockBehavior.Strict);
mock.Setup(foo => foo.Execute("Whatever")).Returns((string)null);
Assert.Null(mock.Object.Execute("Whatever"));
mock.VerifyAll();
}

[Fact]
public void ReturnsNullValueIfNullFuncForStrictMock()
{
var mock = new Mock<IFoo>(MockBehavior.Strict);
mock.Setup(foo => foo.Execute("Whatever")).Returns((Func<string>)null);
Assert.Null(mock.Object.Execute("Whatever"));
mock.VerifyAll();
}

[Fact]
public void ReturnsNullValueIfNullDelegateForStrictMock()
{
var mock = new Mock<IFoo>(MockBehavior.Strict);
mock.Setup(foo => foo.Execute("Whatever")).Returns((Delegate)null);
Assert.Null(mock.Object.Execute("Whatever"));
mock.VerifyAll();
}

[Fact]
public void DifferentMethodCallsReturnDifferentValues()
{
Expand Down
23 changes: 13 additions & 10 deletions Source/MethodCallReturn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,7 @@ public IReturnsResult<TMock> Returns(Delegate valueFunction)
return this.Returns(() => (TResult)(object)valueFunction);
}

// The following may seem overly cautious, but we don't throw an `ArgumentNullException`
// here because Moq has been very forgiving with incorrect `Returns` in the past.
if (valueFunction == null)
{
return this.Returns(() => default(TResult));
}

if (valueFunction.GetMethodInfo().ReturnType == typeof(void))
if (valueFunction != null && valueFunction.GetMethodInfo().ReturnType == typeof(void))
{
throw new ArgumentException(Resources.InvalidReturnsCallbackNotADelegateWithReturnType, nameof(valueFunction));
}
Expand Down Expand Up @@ -154,12 +147,22 @@ IReturnsThrowsGetter<TMock, TResult> ICallbackGetter<TMock, TResult>.Callback(Ac

private void SetReturnDelegate(Delegate value)
{
if (value != null)
if (value == null)
{
// A `null` reference (instead of a valid delegate) is interpreted as the actual return value.
// This is necessary because the compiler might have picked the unexpected overload for calls like `Returns(null)`,
// or the user might have picked an overload like `Returns<T>(null)`,
// and instead of in `Returns(TResult)`, we ended up in `Returns(Delegate)` or `Returns(Func)`,
// which likely isn't what the user intended.
// So here we do what we would've done in `Returns(TResult)`:
this.valueDel = (Func<TResult>)(() => default(TResult));
}
else
{
ValidateReturnDelegate(value);
this.valueDel = value;
}

this.valueDel = value;
this.returnValueKind = ReturnValueKind.Explicit;
}

Expand Down
0