wiremock - 需要进行哪些修改才能使 Wiremock 运行?
问题描述
我有一个名为 ReportService 的 .Net Core Web API 解决方案,它调用另一个 API 端点(我们可以称之为 PayrollService)来获取工资报告。所以我的要求是使用 Wiremock.Net 模拟 PayrollService。
目前我还编写了一个自动化测试用例,它将直接调用 ReportService 控制器并执行所有服务逻辑,以及调用 PayrollService 和 DB 层逻辑的类,并将从 ReportService 中获取 HTTP 结果。
请注意,自动化测试用例是一个单独的解决方案。所以我的要求是像以前一样在 ReportService 上运行自动化测试用例,工资单服务将被 Wiremock 模拟。
那么,代码库中需要进行哪些更改?我们是否必须将 ReportService 的 url 更改为 ReportService 解决方案中的 Wiremock 服务器基本 url?请让我们知道,并请使用我在关于项目名称的问题中使用的术语,以便我清楚。
解决方案
您的假设确实是正确的,您已经制作了可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
}
推荐阅读
- vue.js - Vue3 / Vite:如何打包组件以在 npm 上发布
- reactjs - 如何根据 JS 对象的属性强制 React 组件重新渲染?
- php - 在 MySQL 中每隔 n 小时获取一次订单
- jquery - 修改给定的 jquery 验证器方法以检查 sting 是否不包含 URL
- r - 使用 ggplot2 为前 2 个类别中的每个子类别绘制分组条形图
- java - 使用 DateTimeFormatter 和 SimpleDateFormat 检索格式化日期出错
- google-sheets - 无需拖动即可自动填充一列 - Google 工作表
- php - MappingException - 在链配置的命名空间 xxx 中找不到类 'generalBundle\Entity\xxx' - 升级到 symfony flex
- r - 支持定义函数 - 聚合、均值、t 检验、表格输出
- dataframe - 使用 str 列表 [mean, min, max,...] 从数据帧计算平均值/中值/最小值...