我正在写契约测试,我需要一个可变性。
这是我定义价格对象的实际方法:
private static LambdaDslObject definePrice(LambdaDslObject object) {
return object
.stringType("type") // <--- could be empty or has 2 different values
.stringType("currencyCode", "EUR")
.numberType("centAmount")
.numberType("fractionDigits");
}
但是此价格对象在JSON中看起来像这样:
如果类型为"centPrecision"
或为空:
"value": {
"type": "centPrecision",
"currencyCode": "EUR",
"preciseAmount": 2800,
"fractionDigits": 2 // <--- is not mandatory in this case
}
or
"value": {
"currencyCode": "EUR",
"preciseAmount": 2800,
"fractionDigits": 2 // <--- is not mandatory in this case
}
或者如果类型是"highPrecision"
"value": {
"type": "highPrecision",
"currencyCode": "EUR",
"centAmount": 2800, // <--- is not mandatory in this case
"preciseAmount": 2800,
"fractionDigits": 2
}
所以我需要根据价格类型进行区分。我找到了此or()
方法,但实际上不知道如何实现它。我如何才能实现此协议?
在这种情况下,我将编写两个Pact测试,并使用provider state告知提供者要生产哪个。
每个单独的契约测试都有许多装置。我们将它们称为P,X,Y,Z和O。这些装置用于做出以下断言:
or
,则有两个不同的Z
值。反过来,您将具有两个不同的O值,以及两个不同的X和P值或两个不同的Y值(或两者),具体取决于Z中的类型是否根据请求进行更改。 比方说,Z
中的响应根据提供者状态而改变(更简单的情况)。如果您使用or
,您的测试现在将如下所示使用(任何参数)调用使用者代码时>]
LambdaDslObject
的请求当提供者状态为response is centPrecision
时,产生:
"value": {
"type": "centPrecision",
"currencyCode": "EUR",
"preciseAmount": 2800,
"fractionDigits": 2 // <--- is not mandatory in this case
}
使用(任何参数)调用使用者代码时>]
- 发送
LambdaDslObject
的请求当提供者状态为
response is highPrecision
时,产生:
"value": {
"type": "highPrecision",
"currencyCode": "EUR",
"centAmount": 2800, // <--- is not mandatory in this case
"preciseAmount": 2800,
"fractionDigits": 2
}