首页 > 解决方案 > 需要进行哪些修改才能使 Wiremock 运行?

问题描述

我有一个名为 ReportService 的 .Net Core Web API 解决方案,它调用另一个 API 端点(我们可以称之为 PayrollService)来获取工资报告。所以我的要求是使用 Wiremock.Net 模拟 PayrollService。

目前我还编写了一个自动化测试用例,它将直接调用 ReportService 控制器并执行所有服务逻辑,以及调用 PayrollService 和 DB 层逻辑的类,并将从 ReportService 中获取 HTTP 结果。

请注意,自动化测试用例是一个单独的解决方案。所以我的要求是像以前一样在 ReportService 上运行自动化测试用例,工资单服务将被 Wiremock 模拟。

那么,代码库中需要进行哪些更改?我们是否必须将 ReportService 的 url 更改为 ReportService 解决方案中的 Wiremock 服务器基本 url?请让我们知道,并请使用我在关于项目名称的问题中使用的术语,以便我清楚。

标签: wiremockwiremock-standalonewiremock-record

解决方案


您的假设确实是正确的,您已经制作了可ReportService配置使用的基本 URL。

因此,对于您的单元/集成测试,您可以提供运行 WireMock.Net 服务器的 URL。

例子:

[Test]
public async Task ReportService_Should_Call_External_API_And_Get_Report()
{
  // Arrange (start WireMock.Net server)
  var server = WireMockServer.Start();
  
  // Setup your mapping
  server
    .Given(Request.Create().WithPath("/foo").UsingGet())
    .RespondWith(
      Response.Create()
        .WithStatusCode(200)
        .WithBody(@"{ ""msg"": ""Hello world!"" }")
    );

  // Act (configure your ReportService to connect to the URL where WireMock.Net is running)
  var reportService = new ReportService(server.Urls[0]});
  
  var response = reportService.GetResport();
    
  // Assert
  Assert.Equal(response, ...); // Verify
}

推荐阅读