https://pact.io logo
#pact-jvm
Title
# pact-jvm
n

Nitesh Rajgopal

05/20/2022, 7:30 AM
I have 2 problems here. 1. I cannot repeat objects with the same name "type". They get overwritten.
b

Boris

05/20/2022, 7:32 AM
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

Nitesh Rajgopal

05/20/2022, 7:33 AM
Thanks for the responses. Please advise me on the correct way of doing this
b

Boris

05/20/2022, 7:34 AM
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

Nitesh Rajgopal

05/20/2022, 7:37 AM
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

Boris

05/20/2022, 7:46 AM
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

Nitesh Rajgopal

05/20/2022, 7:52 AM
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

Boris

05/20/2022, 7:58 AM
You could definitely start with what you've got, but with only one
object(...)
in each interaction.
1
n

Nitesh Rajgopal

05/20/2022, 10:48 AM
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

Boris

05/27/2022, 4:31 AM
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