`withCaptured` (mockk) breaks assertion chaining i...
# strikt
e
withCaptured
(mockk) breaks assertion chaining if the slot has not been captured. It throws
Mapping 'captured value %s' failed with: lateinit property captured has not been initialized
. Not sure if this is known or intentional. Would be nice if instead of throwing it would short circuit the block and fail.
maybe something like this?
Copy code
fun <T : Any> Assertion.Builder<CapturingSlot<T>>.betterWithCaptured(
    block: Assertion.Builder<T>.() -> Unit
): Assertion.Builder<CapturingSlot<T>> =
    assertThat("is captured", CapturingSlot<T>::isCaptured)
        .with("captured value %s", CapturingSlot<T>::captured, block)
Or simply
Copy code
fun <T : Any> Assertion.Builder<CapturingSlot<T>>.withCaptured(
    block: Assertion.Builder<T>.() -> Unit
): Assertion.Builder<CapturingSlot<T>> =
    isCaptured().with("captured value %s", CapturingSlot<T>::captured, block)
r
Yep, I’ll get that fixed when I get a chance. Thanks for the report
e
I can’t decide if I like to verify my mocks in the strikt assertions or outside. Playing around w/ this now
Copy code
fun <T> Assertion.Builder<T>.verify(
    ordering: Ordering = Ordering.UNORDERED,
    inverse: Boolean = false,
    atLeast: Int = 1,
    atMost: Int = Int.MAX_VALUE,
    exactly: Int = -1,
    timeout: Long = 0,
    verifyBlock: MockKVerificationScope.() -> Unit
): Assertion.Builder<T> = assert("mockk passes verification") {
    try {
        io.mockk.verify(ordering, inverse, atLeast, atMost, exactly, timeout, verifyBlock)
        pass()
    } catch (e: AssertionError) {
        fail(e.message?.substringBefore("Stack trace:"))
    }
}
r
I tend to only use Strikt when I’m using argument capture (i.e. there’s a complex object passed to the mock that I want to make multiple assertions about. For just verification of a call happening I’m not sure there’s any point in Strikt wrapping a layer around mockk’s API
e
yeah, i think i agree. it just doesn’t match w/ the entire subject -> assertion idiom
r
right
234 Views