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

Difference in behavior when mocking async method using .Result vs without #1253

Closed
cyungmann opened this issue Apr 27, 2022 · 2 comments · Fixed by #1259
Closed

Difference in behavior when mocking async method using .Result vs without #1253

cyungmann opened this issue Apr 27, 2022 · 2 comments · Fixed by #1259
Assignees
Labels
Milestone

Comments

@cyungmann
Copy link

Using Moq 4.17.2, the first two mock setups in the code below work as expected and return a Task<string> that resolves to null, while the third mock setup simply returns null. Is this expected?

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Threading.Tasks;

namespace MoqTest2
{
    public interface IFoo
    {
        Task<string?> Bar();
    }

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public async Task TestMethod1()
        {
            var mock = new Mock<IFoo>();

            mock.Setup(x => x.Bar()).Returns(() => Task.FromResult((string?)null));
            var result = await mock.Object.Bar().ConfigureAwait(false);

            mock.Setup(x => x.Bar()).ReturnsAsync((string?)null);
            result = await mock.Object.Bar().ConfigureAwait(false);

            mock.Setup(x => x.Bar().Result).Returns((string?)null);
            result = await mock.Object.Bar().ConfigureAwait(false);
        }
    }
}

image

@Ana-byte-bit
Copy link

Ana-byte-bit commented Apr 29, 2022

It seems that problem is that Result allowed to have attribute of type TResult instead of Task<TResult>.

@stakx stakx added the bug label May 12, 2022
@stakx
Copy link
Contributor

stakx commented May 12, 2022

This is a bug. As it turns out, the logic that's responsible for wrapping the return value in a completed task only triggers for non-null return values, which is incorrect since null is a perfectly valid value (for reference types and nullable value types).

@stakx stakx self-assigned this May 12, 2022
@stakx stakx added this to the 4.18.1 milestone May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants