如何编写 Pact 合同,以匹配键“x”对应于类型为 Y 或 Z 的对象。

huangapple go评论77阅读模式
英文:

How to write Pact contract that matches to key 'x' either object of type Y or Z

问题

以下是您要求的翻译内容:

我尝试编写一个 Pact 契约测试,涵盖以下场景。我有一个消费者调用 GET 方法来接收有关动物(猫或狗)的信息。

response1:
{
   "animal" : {
      "type" : "Cat",
      "meow" : true
   }
}

response2:
{
   "animal" : {
      "type" : "Dog",
      "barks" : false
   }
}

在代码中,猫和狗都是 Animal 类的子类型。是否有可能编写一个契约,期望响应包含 "animal" 键,其值匹配猫(期望属性 "meow" 和 "type")或狗(期望属性 "barks" 和 "type")。

换句话说,是否有可能在 Pact 的 DSL 中声明在 "animal" 键下可以是定义 X 的对象或定义 Y 的对象?

英文:

I try to write a pact contract test covering following scenario. I have a consumer calling an GET to receive an information about an animal, either a cat or dog.

response1:
{
   "animal" : {
      "type" : "Cat",
      "meow" : true
   }
}

response2:
{
   "animal" : {
      "type" : "Dog",
      "barks" : false
   }
}

In code both Cat and Dog are subtypes of an Animal class. Is there a possibility to write a contract expecting response to contain "animal" key with value matching either cat (expecting properties "meow" and "type") or dog (expecting properties "barks" and "type").

In other words. Is there a possibility inn Pact's Dsl to declare that under the key "animal" there can be either an object of definition X or an object of definition Y?

答案1

得分: 1

不,您必须编写两个单独的测试来涵盖这种情况,以证明您的代码实际上可以处理这两种情况。想象一下,如果您编写了一个单元测试,以Animal作为参数,但根据动物的不同而表现不同 - 您需要测试所有的变体。

这个论点类似于为什么我们不支持可选属性(参见https://docs.pact.io/faq/#why-is-there-no-support-for-specifying-optional-attributes)。

英文:

No, you have to write two separate tests to cover this scenario to prove that your code can actually handle both cases. Think of if you were to write a unit test that took an Animal as an argument, but behaved differently depending on the animal - you'd need to test it with all of the variants.

The argument is similar to why we don't support optional attributes (see https://docs.pact.io/faq/#why-is-there-no-support-for-specifying-optional-attributes).

huangapple
  • 本文由 发表于 2020年10月9日 14:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64275007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定