DorianOlympia
DorianOlympia

Reputation: 851

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

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?

Upvotes: 0

Views: 590

Answers (1)

Matthew Fellows
Matthew Fellows

Reputation: 4065

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).

Upvotes: 1

Related Questions