我正在开发人员解决方案中为一项服务编写测试,并且我正在使用 pact 模拟服务模拟其他第三方服务。我需要验证发送到此模拟服务的请求。所以我需要获取实际发送的有效载荷。(实际存储在日志文件中的那个以“收到的请求”开头)
我将非常感谢您的帮助
Pact 会为您验证消费者请求。看看这个例子。
对于消费者来说,测试过程是:
我将第 5 步加粗,因为这是检查请求的步骤。
(另请注意,如果请求不正确,则第 3 步不会生成正确的响应,因此第 4 步几乎总是会失败)
这是我上面链接到我刚刚列出的消费者测试步骤的示例的细分:
_mockProviderService
.Given("There is a something with id 'tester'")
.UponReceiving("A GET request to retrieve the something")
.With(new ProviderServiceRequest
{
Method = HttpVerb.Get,
Path = "/somethings/tester",
Headers = new Dictionary<string, object>
{
{ "Accept", "application/json" }
}
})
.WillRespondWith(new ProviderServiceResponse
{
Status = 200,
Headers = new Dictionary<string, object>
{
{ "Content-Type", "application/json; charset=utf-8" }
},
Body = new //NOTE: Note the case sensitivity here, the body will be serialised as per the casing defined
{
id = "tester",
firstName = "Totally",
lastName = "Awesome"
}
}); //NOTE: WillRespondWith call must come last as it will register the interaction
var consumer = new SomethingApiClient(_mockProviderServiceBaseUri);
//Act
var result = consumer.GetSomething("tester");
//Assert
Assert.Equal("tester", result.id);
_mockProviderService.VerifyInteractions(); //NOTE: Verifies that interactions registered on the mock provider are called once and only once
^ 这是您验证发送的请求是否正确所需的步骤。