如何在dius.pact中实现“ or()”方法

问题描述 投票:0回答:1

我正在写契约测试,我需要一个可变性。

这是我定义价格对象的实际方法:

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()方法,但实际上不知道如何实现它。我如何才能实现此协议?

java spring integration-testing pact
1个回答
0
投票

在这种情况下,我将编写两个Pact测试,并使用provider state告知提供者要生产哪个。

理论上

每个单独的契约测试都有许多装置。我们将它们称为PXYZO。这些装置用于做出以下断言:

  1. 使用某些参数调用使用者代码时,P
  2. 然后将请求X发送给提供者
  3. 当提供者处于状态[[Y时,它会产生响应Z
  4. 当使用者收到响应
  5. Z
  6. 时,域对象O从步骤1返回给调用者。
如果测试中有or,则有两个不同的

Z

值。反过来,您将具有两个不同的O值,以及两个不同的XP值或两个不同的Y值(或两者),具体取决于Z中的类型是否根据请求进行更改。 比方说,

Z

中的响应根据提供者状态而改变(更简单的情况)。如果您使用or,您的测试现在将如下所示
    使用某些参数调用使用者代码时,
  1. P
然后将请求
  • X
  • 发送给提供者当提供者处于状态[[Y-1时,它会产生响应
  • Z-1,或者如果它处于状态[[Y-2中,则它会产生响应Z-2
  • 当使用者收到响应Z-1时,域对象O-1从步骤1返回给调用者,或者如果使用者收到响应
  • Z-2
  • ,则域对象< [O-2返回给呼叫者。这比分成两个测试要复杂得多。两个测试测试一个可能类似于:

    使用(任何参数)调用使用者代码时>]

    发送LambdaDslObject的请求

    当提供者状态为response is centPrecision时,产生:

      "value": { "type": "centPrecision", "currencyCode": "EUR", "preciseAmount": 2800, "fractionDigits": 2 // <--- is not mandatory in this case }
  • 哪个将适当的域对象返回给调用者。
  • 并且测试两个可能类似于:
      使用(任何参数)调用使用者代码时>]
    1. 发送LambdaDslObject的请求

      当提供者状态为response is highPrecision时,产生:

    1. "value": { "type": "highPrecision", "currencyCode": "EUR", "centAmount": 2800, // <--- is not mandatory in this case "preciseAmount": 2800, "fractionDigits": 2 }
    2. 另外,请记住,您的Pact测试应为describe only the response fields the consumer actually uses-不应作为对提供者返回的字段的完整描述。因此,您可以从响应中删除非强制性字段。
    © www.soinside.com 2019 - 2024. All rights reserved.