如何解析协议提供者验证时的响应
how to parse the response at pact provider verfication
我的场景是消费者定义了两个交互,interation1 与 state1,interation2 与 state2。在运行interation1之后,JSON响应中会有一个id,这个id必须作为interation2的query参数。在提供商方面,有没有办法从 interation1 的响应中提取该 ID?顺便说一下,我正在使用契约提供者 junit。
你的想法是错误的。
Provider states 旨在防止这种形式的耦合。交互 1 应该完全独立于交互 2。
Tests that depend on the outcome of previous tests are brittle and land you back in integration test hell, which is the nasty place you're trying to escape by using pacts.
您必须控制提供程序测试上下文才能使 Pact 测试正常工作。
在您的情况下,对于状态 2,您可能具有以下描述:
a user with ID 1 exists
在测试该特定交互之前,Pact 将 provide you 有机会设置该状态,但是这应该针对您的用例完成 - 例如您的代码可以使用该 ID 在数据库(或内存中的资源)中创建资源。
@State("a user with ID 1 exists") // Must match the state description in the pact file
public void setupUser1() {
// Do what you need to for that user to exist
}
这样一来,交互 2 可能会在完全不知道交互 1 存在的情况下执行。
我的场景是消费者定义了两个交互,interation1 与 state1,interation2 与 state2。在运行interation1之后,JSON响应中会有一个id,这个id必须作为interation2的query参数。在提供商方面,有没有办法从 interation1 的响应中提取该 ID?顺便说一下,我正在使用契约提供者 junit。
你的想法是错误的。
Provider states 旨在防止这种形式的耦合。交互 1 应该完全独立于交互 2。
Tests that depend on the outcome of previous tests are brittle and land you back in integration test hell, which is the nasty place you're trying to escape by using pacts.
您必须控制提供程序测试上下文才能使 Pact 测试正常工作。
在您的情况下,对于状态 2,您可能具有以下描述:
a user with ID 1 exists
在测试该特定交互之前,Pact 将 provide you 有机会设置该状态,但是这应该针对您的用例完成 - 例如您的代码可以使用该 ID 在数据库(或内存中的资源)中创建资源。
@State("a user with ID 1 exists") // Must match the state description in the pact file
public void setupUser1() {
// Do what you need to for that user to exist
}
这样一来,交互 2 可能会在完全不知道交互 1 存在的情况下执行。