assertk icon indicating copy to clipboard operation
assertk copied to clipboard

Feature to peek into an assertion

Open andriesfc opened this issue 3 years ago • 1 comments

I've been adding this simple extension function to peek into my assertions:

fun <T> Assert<T>.peek(peekActual: (T) -> Unit): Assert<T> = apply { given(peekActual) }

I use this function mostly to inspect and print out more detailed messages without stopping the assertion logic flow, for example:

 @Test
  fun testRaisingAsException() {
      assertThat { raise(TransactionError.StopEndOfFile) }
          .isFailure().peek { it.printStackTrace() }
          .isInstanceOf(TransactionError.TransactionException::class)
  }

I would like to contribute back to the community as I feel, even such as simple function has great utility to debug any complex assertion flow.

andriesfc avatar Aug 08 '21 05:08 andriesfc

Thanks - this is very useful!

I also create this method:

fun <T> Assert<T>.value(): T {
  given {
    return it
  }

  throw IllegalStateException("This should never be reached")
}

jschneider avatar Mar 08 '24 17:03 jschneider