Skip to content
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

Fix type mismatch during callback validation for task .Result setups #1133

Merged
merged 4 commits into from
Jan 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1

* `AmbiguousMatchException` raised when interface has property indexer besides property in VB. (@mujdatdinc, #1129)
* Interface default methods are ignored (@hahn-kev, #972)
* Callback validation too strict when setting up a task's `.Result` property (@stakx, #1132)


## 4.16.0 (2021-01-16)

Expand Down
9 changes: 6 additions & 3 deletions src/Moq/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Moq.Behaviors;
using Moq.Properties;

using TypeNameFormatter;

namespace Moq
{
internal sealed partial class MethodCall : SetupWithOutParameterSupport
Expand Down Expand Up @@ -310,7 +312,8 @@ void ValidateCallback(Delegate callback)
throw new ArgumentException(Resources.InvalidReturnsCallbackNotADelegateWithReturnType);
}

var expectedReturnType = this.Method.ReturnType;
var expectedReturnType = this.Expectation.HasResultExpression(out var awaitable) ? awaitable.ResultType
: this.Method.ReturnType;

if (!expectedReturnType.IsAssignableFrom(actualReturnType))
{
Expand All @@ -321,8 +324,8 @@ void ValidateCallback(Delegate callback)
string.Format(
CultureInfo.CurrentCulture,
Resources.InvalidCallbackReturnTypeMismatch,
expectedReturnType,
actualReturnType));
expectedReturnType.GetFormattedName(),
actualReturnType.GetFormattedName()));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Moq/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Remember that there's no generics covariance in the CLR, so your object must be
<value>Type {0} does not have matching protected member: {1}</value>
</data>
<data name="InvalidCallbackReturnTypeMismatch" xml:space="preserve">
<value>Invalid callback. Setup on method with return type ({0}) cannot invoke callback with return type ({1}).</value>
<value>Invalid callback. Setup on method with return type '{0}' cannot invoke callback with return type '{1}'.</value>
</data>
<data name="InvalidCallbackParameterCountMismatch" xml:space="preserve">
<value>Invalid callback. Setup on method with {0} parameter(s) cannot invoke callback with different number of parameters ({1}).</value>
Expand Down
18 changes: 18 additions & 0 deletions tests/Moq.Tests/SetupTaskResultFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public async Task Setup__completed_Task__Returns()
Assert.Same(Friend, friend);
}

[Fact]
public async Task Setup__completed_Task__Returns_callback()
{
var person = new Mock<IPerson>();
person.Setup(p => p.GetFriendTaskAsync().Result).Returns(() => Friend);
var friend = await person.Object.GetFriendTaskAsync();
Assert.Same(Friend, friend);
}

[Fact]
public async Task Setup__completed_Task__Throws()
{
Expand All @@ -119,6 +128,15 @@ public async Task Setup__completed_ValueTask__Returns()
Assert.Same(Friend, friend);
}

[Fact]
public async Task Setup__completed_ValueTask__Returns_callback()
{
var person = new Mock<IPerson>();
person.Setup(p => p.GetFriendValueTaskAsync().Result).Returns(()=> Friend);
var friend = await person.Object.GetFriendValueTaskAsync();
Assert.Same(Friend, friend);
}

[Fact]
public async Task Setup__completed_ValueTask__Throws()
{
Expand Down