我正在开发人员解决方案中为一项服务编写测试,而我正在使用pact模拟服务来模拟其他第三方服务。 我需要验证发送到此模拟服务的请求。所以我需要获取实际发送的有效负载。 (实际存储在以“已接收请求”开头的日志文件中的那个)
我非常感谢帮助
答案 0 :(得分:0)
Pact为您验证消费者请求。看看this example。
对于消费者而言,测试过程是:
我已经加粗了第5步,因为这是检查请求的步骤。
(另请注意,如果请求不正确,步骤3不会生成正确的响应,因此步骤4几乎总是会失败)
此处将the example I linked above细分为我刚刚列出的消费者测试步骤:
_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
^这是验证发送的请求是否正确所需的步骤。