【问题标题】:Using Moq to mock an asynchronous method for a unit test使用 Moq 模拟单元测试的异步方法
【发布时间】:2014-01-18 12:36:07
【问题描述】:

我正在测试一种用于进行 Web API 调用的服务的方法。如果我还在本地运行 Web 服务(位于解决方案中的另一个项目中),则使用普通的 HttpClient 可以很好地进行单元测试。

但是,当我签入我的更改时,构建服务器将无法访问 Web 服务,因此测试将失败。

我通过创建IHttpClient 接口并实现我在我的应用程序中使用的版本,为我的单元测试设计了一种解决方法。对于单元测试,我使用模拟的异步 post 方法制作了一个完整的模拟版本。这是我遇到问题的地方。我想为这个特定的测试返回一个 OK HttpStatusResult。对于另一个类似的测试,我将返回一个糟糕的结果。

测试将运行但永远不会完成。它挂在等待。我是异步编程、委托和 Moq 本身的新手,我一直在搜索 SO 和谷歌一段时间来学习新事物,但我似乎仍然无法解决这个问题。

这是我要测试的方法:

public async Task<bool> QueueNotificationAsync(IHttpClient client, Email email)
{
    // do stuff
    try
    {
        // The test hangs here, never returning
        HttpResponseMessage response = await client.PostAsync(uri, content);

        // more logic here
    }
    // more stuff
}

这是我的单元测试方法:

[TestMethod]
public async Task QueueNotificationAsync_Completes_With_ValidEmail()
{
    Email email = new Email()
    {
        FromAddress = "bob@example.com",
        ToAddress = "bill@example.com",
        CCAddress = "brian@example.com",
        BCCAddress = "ben@example.com",
        Subject = "Hello",
        Body = "Hello World."
    };
    var mockClient = new Mock<IHttpClient>();
    mockClient.Setup(c => c.PostAsync(
        It.IsAny<Uri>(),
        It.IsAny<HttpContent>()
        )).Returns(() => new Task<HttpResponseMessage>(() => new HttpResponseMessage(System.Net.HttpStatusCode.OK)));

    bool result = await _notificationRequestService.QueueNotificationAsync(mockClient.Object, email);

    Assert.IsTrue(result, "Queue failed.");
}

我做错了什么?

感谢您的帮助。

【问题讨论】:

    标签: c# unit-testing asynchronous moq


    【解决方案1】:

    使用Mock.Of&lt;...&gt;(...)async 方法,您可以使用Task.FromResult(...)

    var client = Mock.Of<IHttpClient>(c => 
        c.PostAsync(It.IsAny<Uri>(), It.IsAny<HttpContent>()) == Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))
    );
    

    【讨论】:

      【解决方案2】:

      在上面推荐@Stuart Grassie 的答案。

      var moqCredentialMananger = new Mock<ICredentialManager>();
      moqCredentialMananger
                          .Setup(x => x.GetCredentialsAsync(It.IsAny<string>()))
                          .ReturnsAsync(new Credentials() { .. .. .. });
      

      【讨论】:

        【解决方案3】:

        您正在创建一项任务,但从未启动它,因此它永远不会完成。但是,不要只是开始任务 - 而是改为使用 Task.FromResult&lt;TResult&gt;,这将为您提供已经完成的任务:

        ...
        .Returns(Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK)));
        

        请注意,您不会以这种方式测试实际的异步性 - 如果您想这样做,您需要做更多的工作来创建一个Task&lt;T&gt;,您可以以更细粒度的方式控制它。 ..但那是另一天的事情了。

        您可能还想考虑为IHttpClient 使用假货,而不是模拟所有内容 - 这实际上取决于您需要它的频率。

        【讨论】:

        • 非常感谢。那效果很好。我想这可能是一些我不理解的简单事情。
        • Re: Fake IHttpClient,我考虑过,但我需要能够根据从 Web API 返回的预期行为为不同的测试返回不同的 HttpStatusCodes,这似乎给了我更多的控制权。
        • @mvanella:是的,所以你会创建一个可以返回任何你想要它的假货。只是想一想。
        • 对于现在发现这个的任何人,Moq 4.2 有一个名为 ReturnsAysnc 的扩展,它就是这样做的。
        • @legacybass 我找不到指向它的任何文档的链接,尽管 API 文档说它们是针对 v4.2.1312.1622 构建的,几乎是一年前的 released。请参阅发布前几天制作的this commit。至于为什么 API 文档没有更新...
        猜你喜欢
        • 2020-12-01
        • 2015-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2020-07-14
        相关资源
        最近更新 更多