I have 2 problems here. 1. I cannot repeat objects...
# pact-jvm
n
I have 2 problems here. 1. I cannot repeat objects with the same name "type". They get overwritten.
b
Yeah, that's not how
arrayEachLike
works. It takes a single object for a number of repetitions, not a heterogeneous set of objects.
If you look closely, this is an array with one big object inside, not two.
n
Thanks for the responses. Please advise me on the correct way of doing this
b
What interaction are you trying to describe? Because that will influence what goes into your requests/responses 🙂
i.e., do you really need multiple objects with different fields present, or could that be split to more specific interactions?
In general, the
eachLike
functions in Pact are for testing lists of objects with the same shape.
n
So, I will have multiple type objects within the response array object, each with their unique properties, and I will need to validate that each of them and their properties exist
b
that's definitely one way to do it, and probably the way I'd suggest if you need that kind of reasoning power
other ways include using the
arrayContaining
function (from v4), but that gives you a different intent profile
n
Would you mind getting me started with a code snippet please. I can't use v4 at the moment, so I will need to stick with the previous option you mentioned
b
You could definitely start with what you've got, but with only one
object(...)
in each interaction.
1
n
Thanks for your guidance. I finally made some progress but still not using the lambda syntax. Might convert it once I get the contract right
b
On the Kotlin + lambda syntax, my tests tend to look something like this, with help from many extension functions:
Copy code
val pact = buildPact(given = "several amendments") {
  uponReceiving("a request to begin reindexing amendments").run {
    method("POST").path("/admin/amendments/reindex").authHeader()
  }.willRespondWith().run {
    status(201) jsonBody {
      uuid("transactionId", ConsistentUUID)
      zonedDateTime("startDate")
      numberType("pending", 300)
      numberType("successes", 0)
      numberType("failures", 0)
      numberType("total", 300)
    }
  }
}

val response = pact {
  startAmendmentsReindex()
}
extension helpers like these:
Copy code
fun pactBuilder() = ConsumerPactBuilder
  .consumer("redacted-consumer")
  .hasPactWith("redacted-api")

fun buildPact(
  builder: PactDslWithProvider.() -> PactDslResponse
): RequestResponsePact = pactBuilder().builder().toPact()

fun buildPact(
  given: String,
  builder: PactDslWithState.() -> PactDslResponse
): RequestResponsePact = pactBuilder().given(given).builder().toPact()

operator fun <R> BasePact.invoke(auth: Boolean = true, createCall: AdminClient.() -> Call<R>): R? {
  println()
  val result: PactVerificationResult = runConsumerTest(
    this,
    MockProviderConfig.createDefault(),
    object : PactTestRun<Response<R>> {
      override fun run(mockServer: MockServer, context: PactTestExecutionContext?): Response<R> {
        val client = buildClient<AdminClient>(mockServer.getUrl(), auth)
        return client.createCall().execute()
      }
    }
  )
  println("testPact result: $result")
  return ((result as? Ok)?.result as? Response<R>?)?.body()
}

infix fun PactDslResponse.jsonBody(bodyBuilder: LambdaDslJsonBody.() -> Unit): PactDslResponse =
  body(LambdaDsl.newJsonBody(bodyBuilder).build())

fun PactDslResponse.jsonArray(times: Int = 1, body: LambdaDslObject.() -> Unit): PactDslResponse =
  body(newJsonArray { repeat(times) { newObject(body).build() } })
etc
👍 1
1