Hello everyone. I'm attempting to create a body D...
# pact-jvm
g
Hello everyone. I'm attempting to create a body DSL closure to generate a contract. I am creating the closure using fields and an array. However, I would like the array to have matchers to uniquely match each index with a different object.
eachLike
only works when an array has the same object in each index. Here is an example JSON that needs to be matched :
Copy code
{
  "version": 1234,
  "data": [
    {
      "name": "someName"
    },
    {
      "name": "anotherName",
      "id": 131312
    }
  ]
}
As you see using an eachLike on data wouldn't work because of the extra
id
field. Does anyone know how I can achieve this? I've look at the documentation, but I can't seem to figure it out. Thanks!
m
I think there is another matcher called something like "array containing"
That might be better
g
I tried using that but it doesn't populate the contract for the field in there
m
can you please elaborate?
g
Yes, when I use arrayContaining matcher in the
withBody
closure. ex:
Copy code
.withBody({
 data arrayContaining({
         name string()
        }, {
         name string()
         id   integer()
      })
})
This generates my JSON contract but with the
data
key empty
data : {}
Instead of being populated
If I use
eachLike
it will populate that field with correct fields/values array
But I don't want to use this because of that second entry that is different
m
I can’t really help in terms of how to write that test in java/groovy, but that sounds like a bug
it should definitely return an array with data in it. So either, the input to the matcher is incorrect or there is a bug in the framework
Hopefully somebody else can steer you with how to use that matcher in groovy