Writing PACT test might be quite a challenge in the beginning, as it is a new tool, and we might not be able to find much information in the web yet.
Personally, I find the PACT Google Support Group is very helpful when I encounter issues. Otherwise, sometimes I do some trial and error to solve my obstacles. Below are some quick solutions to some consumer/provider test issues that I have encountered.
Consumer Tests
Below are some scenarios when it comes to writing Body DSL for some JSON bodies.
Time stamp with certain format
Eg: 2015-05-13T06:18:06Z
Body DSL: yyyy-MM-dd’T’HH:mm:ssX
Negative integer
Eg: negative integer: -280
Body DSL: integer(-250)
Field with empty []
Eg: factory[]
Body DSL: factory([])
JSON body inside arrays
Eg: factory[{
field1 123
field2 12.5
}]
Body DSL:
factory([{
field1 integer()
field2 real()
}])
A field which might return an empty value
Eg:
“fruit”, a String which can be an empty String sometimes
Body DSL:
“fruit” (~/\w.+|^$/,”banana or apple”)
Note that we could use an or operator inside the regex. However, it is probably better to separate this into 2 test cases.
Provider Test
Unable to call API in Provider test via load balancer
Eg:
API connection fails when we call an API via a URL that goes through load balancer (not sure why)
Workaround:
Call the API directly via the server, bypassing load balancer
Leave a Reply