Hey folks, I'm still trying to correctly test coll...
# protobufs
s
Hey folks, I'm still trying to correctly test collections. I can do something like this, which seems to mean `objs should contain at least one item, each matching "obj" below`:
Copy code
// provider state: given obj exists
{
	"objs": {
		"pact:match": "eachValue(matching($'obj')),atLeast(1)",
		"obj": {
			"field": "matching(type, 'sample')"
		}
	}
}
which is cool and exactly what I want to express. However, if, theoretically speaking, my client code had branching like
if len(objs) == 1 {} else {}
we would want our client code to hit both branches. Would we want a separate consumer test for that? What should we specify? I guess we could do provider state define multiple objects, but what would I put in the matcher then?
r
I wouldn't do that in a contract test. The contract test should ensure the shape of things. I would add unit tests to then assert the behavior of the client if it receives 1, 2 ... n items
thankyou 2 1
👍 1
s
makes sense, thank you. So as far as contract test is concerned, is the syntax above correct? Also, would you recommend having a separate contract for a provider state "no objs exist" and receiving an empty list?
r
The syntax looks correct to me
If an empty list is a valid value, I would remove the constraint in your contract test and add a unit test for handling 0 items. Then I would add a provider state that will ensure the provider sends some items when the contract is verified so the shape of the items can be checked.
s
So a single test that specifies a provider state "given an item exists" but without the
atLeast
constraint is the best option, if you also have unit tests validating the client code for edge conditions, is that correct?
r
Well, that is what I would do. There is no right answer.
s
Thanks! I think we'll go with your experience 🙂