【问题标题】:What are the modifications needed to be done to get Wiremock running?需要进行哪些修改才能使 Wiremock 运行?
【发布时间】:2021-01-14 17:39:38
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: wiremock wiremock-standalone wiremock-record


    【解决方案1】:

    您的假设确实正确,您已将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
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 2015-03-25
      • 2011-05-02
      相关资源
      最近更新 更多