0

I am aware of that PACT expects provider data need to be in our control, but I am facing following situation I have pact contract for multiple consumers, all have some mandatory attribute and some are optional attribute, but business logic suppress all the attribute which are having null value, but according to contract I would still be needing that value as null, what should I do for it? Edit 1: i.e let's say below my contract looks

consumer sent request with below params:
{ "method": "GET", "path" : "/pathOfApi", "headers":{ "accept": "json" } }

Provider responds with below data:

{ "Status": 200,
"body" :[
{"country" : "string",
"countryId" :"string",
"postalcode": "string",
"addressLine1" :"string",
"addressLine2" : "string"
"customerName" : "string",
"customerId" : "string"
}
]

now not all customer has address line 2, now in production if addressLine 2 is null it won't be present in output of api, but for our contract field should be present with null

LowCool
  • 1,187
  • 5
  • 25
  • 51

1 Answers1

0

If your provider does not return a field, but the consumer is expecting null, then either the consumer needs to change their expectation (because it's incorrect) or the provider should update its implementation to return null values.

Just because a consumer asks for something doesn't mean you need to do it!

If in some instances the field is present and other it is not, you need to write two tests to cover each case. I'd suggest covering one case with all of the fields, and another with the minimum set of fields (see https://docs.pact.io/faq/#why-is-there-no-support-for-specifying-optional-attributes).

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • sorry not getting you..in my case consumer gives contract of i.e 15 fields out of that sometime all are present and sometime not..so what provider does is whichever fields are not present for that id, it doesn't give to consumer, but it can present for different id – LowCool Sep 22 '21 at 17:15
  • I'm sorry, I think I'm not understanding your situation. Perhaps could you please explain with code examples and payloads? This way we can clarify things for you. It does sound a bit like needing to test various states (data present / absent) which you would usually use Provider States for. – Matthew Fellows Sep 23 '21 at 22:42
  • updated,. can you please check now – LowCool Sep 24 '21 at 17:08
  • Updated based on your new questions details – Matthew Fellows Sep 25 '21 at 21:12
  • So I need to create 2 contracts? And just thinking if I can control database behavior i.e mocking db.. – LowCool Sep 27 '21 at 18:25
  • Yes, you'll need to create two cases. If we only test the case where it is null, we can't be sure what the value is when it's not null and if your consumer can handle it. – Matthew Fellows Sep 28 '21 at 01:02
  • ok..so way to mock or control db?\ – LowCool Sep 28 '21 at 07:24
  • 1
    Pact is a unit testing tool, so it is assumed you have full control over your code as you would as part of a unit test. See our first AMA which covered this (https://docs.pact.io/help/amas/#apac-1) but also our docs. – Matthew Fellows Sep 29 '21 at 08:18
  • so in that case I need to ask my all consumers to provide 2 separate contracts to cover all scenario? – LowCool Oct 04 '21 at 16:03
  • Yes, but of course only if they need to support that scenario. If you have a large number of consumers, you could use the bi-directional pacts feature of Pactflow (https://docs.pactflow.io/docs/workshops/bi-directional) which would simplify this. – Matthew Fellows Oct 05 '21 at 06:36