zio, custom Assertion renderer for current value

Viewed 82

We have a custom zio.test.Assertion that wrap json-path-assert:

  def isJson(matching: org.hamcrest.Matcher[_ >: ReadContext]): Assertion[HttpEntity] = {
    def test(entity: => HttpEntity): Boolean = {
      // Removed for brevity
    }
    Assertion.assertion[HttpEntity]("isJson")(param(matching))(test)
  }

We are quite happy with it but the failure message is far to be obvious:

response.body = Strict(ByteString(121, 32, 95, 111, 10, 108, 113, 35, 50, 90, 124, 23, 112, 111, 112, 31, 48, 24, 46, 31, 111, 22, 111, 25, 93, 52, 41, 54, 59, 90, 100, 43, 42, 51, 75, 14, 41, 54, 40, 65, 34, 42, 99, 88, 100, 54, 34, 25, 33, 89, 41, 40, 54, 52, 44, 40, 31, 101, 112, 105, 91, 10, 35, 125, 79, 106, 95, 11, 11, 32, 85, 13, 43, 104, 111)... and [661] more,Some(application/json)) did not satisfy isJson(with json path "$['store']['name']")

I would like to print the Json string decoded from those bytes but I do not see how to do that.

How can we customize the messages of a custom zio.test.Assertion?

Thanks

1 Answers

The param(matching) means that you are using the toString method of matching to render the the input of the assertion.

You can pass the decoded json string instead to have that display. For example (assuming your json is UTF-8 encoded):

 Assertion.assertion[HttpEntity.Strict]("isJson")
     (param(matching.data.decodeString(StandardCharsets.UTF_8))(test)

That particular example only works on HttpEntity.Strict but can be generalized to HttpEntity if you access the entity contents with .dataStream

Related