【问题标题】:Wiremock.net not responding in unit testsWiremock.net 在单元测试中没有响应
【发布时间】:2017-12-15 08:37:50
【问题描述】:

我一直在使用 Wiremock.net 来模拟 HTTP 调用。我已经让它从控制台应用程序中愉快地运行,但是当我尝试使用单元测试来完成它时,它会停止响应。这是我的完整代码:

FluentMockServer serviceMock;

[TestInitialize]
public void Initialize()
{
    serviceMock = FluentMockServer.Start(new FluentMockServerSettings { Urls = new[] { "http://+:5010" } });
    serviceMock.Given(Request.Create().UsingPost().WithPath("/callbacks/booked"))
        .RespondWith(Response.Create().WithStatusCode(200).WithHeader("Content-Type", "application/json")
            .WithBodyAsJson(new List<Callback>
            {
                new Callback { CustomerId = 1, RequestedFor = new DateTime(2017, 12, 14, 15, 00, 01) },
            }));
}

[TestMethod]
public void CanHitMockEndpoint()
{
    var client = new RestClient("http://localhost:5010");
    var response = client.Get<List<Callback>>(new RestRequest("/callbacks/booked"));

    response.StatusCode.Should().Be(HttpStatusCode.OK);
}

[TestCleanup]
public void Cleanup() => serviceMock.Stop();

当我收到响应时,它的状态码为零,并且消息“无法连接到远程服务器”

奇怪的是,如果我将 Initialize 和 Cleanup 项移动到控制台应用程序中,并同时运行它,测试将起作用。我已经确认 Initialize/Cleanup 正在运行,所以我肯定遗漏了一些明显的东西,或者正在发生一些奇怪的事情!

任何帮助都会很棒。

【问题讨论】:

  • 我知道这是一个老问题,但为了将来参考,您指定的端口很可能已经在使用中,在这种情况下,wiremock 将使用不同的端口。

标签: c# unit-testing wiremock


【解决方案1】:

我仍然不完全确定问题出在哪里,但我已将其范围缩小到端口的处理。我已将服务器创建修改为:

serviceMock = FluentMockServer.Start();

并使用它在内部连接的 IP 进行我的服务设置:

var client = new RestClient(serviceMock.Urls.First());

这样就可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多