Skip to content

Commit

Permalink
Merge pull request #1259 from stakx/async-null-results
Browse files Browse the repository at this point in the history
Allow async `.Result` setups to return `null`
  • Loading branch information
stakx committed May 12, 2022
2 parents a569016 + 2cfc109 commit 20eb04a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


## Unreleased

#### Fixed

* Difference in behavior when mocking async method using `.Result` vs without (@cyungmann, #1253)


## 4.18.0 (2022-05-12)

New major version of DynamicProxy (you may get better performance!), so please update with care.
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Invocation.cs
Expand Up @@ -97,7 +97,7 @@ public void ConvertResultToAwaitable(IAwaitableFactory awaitableFactory)
{
this.result = awaitableFactory.CreateFaulted(r.Exception);
}
else if (this.result != null && !this.method.ReturnType.IsAssignableFrom(this.result.GetType()))
else if (!this.method.ReturnType.IsAssignableFrom(this.result?.GetType()))
{
this.result = awaitableFactory.CreateCompleted(this.result);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Expand Up @@ -3802,6 +3802,29 @@ public void No_ArgumentException_due_to_parameter_refness()

#endregion

#region 1253

public class Issue1253
{
public interface IFoo
{
Task<string> Bar();
}

[Fact]
public async Task Test()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Bar().Result).Returns((string)null);

var result = await mock.Object.Bar();

Assert.Null(result);
}
}

#endregion

// Old @ Google Code

#region #47
Expand Down

0 comments on commit 20eb04a

Please sign in to comment.