消费者测试的验证不匹配
Verification mismatches for Consumer Test
当前在 运行 对消费者端进行 Pact 测试时出现此错误:
Pact verification failed. See output for details.
Output:
Verification mismatches:
[{"method": "GET", "path": "/todos", "request": {"headers": {"Accept": "application/json"}, "method": "GET", "path": "/todos"}, "type": "missing-request"}]
Mock server logs:
这就是我构建消费者合同测试的方式:
public class TodoControllerTests
{
private readonly IPactBuilderV3 pactBuilder;
private readonly List<object> todos;
public TodoContractTests(ITestOutputHelper output)
{
Todos = new List<object>()
{
New {
Id = 1,
Description = "Create Consumer App",
isDone = false,
createdDate = "2022-05-02T00:00:00"
}
New {
Id = 2,
Description = "Create Provider App",
isDone = false,
createdDate = "2022-03-21T00:00:00"
}
};
var config = new PactConfig
{
PactDir = "../../../pacts/"
Outputters = new []
{
New XUnitOutput(output)
},
DefaultJsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
},
};
IPactV3 pact = Pact.V3("Something API Consumer", "Something API", config);
this.pactBuilder = pact.UsingNativeBackend();
}
[Fact]
public async void Should_get_all_todos()
{
this.pactBuilder
.UponReceiving("A GET request to retrieve todos")
.Given("There is a list of todos")
.WithRequest(HttpMethod.Get, "/todos")
.WithHeader("Accept", "application/json")
.WillRespond()
.WithStatus(HttpStatusCode.OK)
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithJsonBody(new TypeMatcher(todos));
await this.pactBuilder.VerifyAsync(async ctx =>
{
var client = new HttpClient
{
BaseAddress = new Uri("http://localhost:55907")
};
var todoApiProxy = new TodoApiProxy(client);
var response = await todoApiProxy.GetAll();
});
}
}
我已验证服务器返回的响应与我在该待办事项对象中定义的响应相同,因此不确定我在验证模式中缺少什么。
问题是您的 HttpClient
正在向 http://localhost:55907
发送请求,而不是实际的 Pact Mock 服务。
错误很明显 - 它没有收到请求。
传给VerifyAsync
的ctx
有需要发送请求的动态模拟服务器的地址。
您的代码可以修改为:
var client = new HttpClient
{
BaseAddress = new Uri(ctx.MockServerUri)
};
...
当前在 运行 对消费者端进行 Pact 测试时出现此错误:
Pact verification failed. See output for details. Output: Verification mismatches: [{"method": "GET", "path": "/todos", "request": {"headers": {"Accept": "application/json"}, "method": "GET", "path": "/todos"}, "type": "missing-request"}] Mock server logs:
这就是我构建消费者合同测试的方式:
public class TodoControllerTests
{
private readonly IPactBuilderV3 pactBuilder;
private readonly List<object> todos;
public TodoContractTests(ITestOutputHelper output)
{
Todos = new List<object>()
{
New {
Id = 1,
Description = "Create Consumer App",
isDone = false,
createdDate = "2022-05-02T00:00:00"
}
New {
Id = 2,
Description = "Create Provider App",
isDone = false,
createdDate = "2022-03-21T00:00:00"
}
};
var config = new PactConfig
{
PactDir = "../../../pacts/"
Outputters = new []
{
New XUnitOutput(output)
},
DefaultJsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
},
};
IPactV3 pact = Pact.V3("Something API Consumer", "Something API", config);
this.pactBuilder = pact.UsingNativeBackend();
}
[Fact]
public async void Should_get_all_todos()
{
this.pactBuilder
.UponReceiving("A GET request to retrieve todos")
.Given("There is a list of todos")
.WithRequest(HttpMethod.Get, "/todos")
.WithHeader("Accept", "application/json")
.WillRespond()
.WithStatus(HttpStatusCode.OK)
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithJsonBody(new TypeMatcher(todos));
await this.pactBuilder.VerifyAsync(async ctx =>
{
var client = new HttpClient
{
BaseAddress = new Uri("http://localhost:55907")
};
var todoApiProxy = new TodoApiProxy(client);
var response = await todoApiProxy.GetAll();
});
}
}
我已验证服务器返回的响应与我在该待办事项对象中定义的响应相同,因此不确定我在验证模式中缺少什么。
问题是您的 HttpClient
正在向 http://localhost:55907
发送请求,而不是实际的 Pact Mock 服务。
错误很明显 - 它没有收到请求。
传给VerifyAsync
的ctx
有需要发送请求的动态模拟服务器的地址。
您的代码可以修改为:
var client = new HttpClient
{
BaseAddress = new Uri(ctx.MockServerUri)
};
...