doc / ch.tutteli.atrium.api.infix.en_GB / cause

cause

inline fun <reified TExpected : Throwable> Expect<out Throwable>.cause(): Expect<TExpected> (source)

Expects that the property Throwable.cause of the subject is a TExpected (the same type or a sub-type), creates an Expect of the TExpected type for it and returns it.

expect(IllegalStateException(IndexOutOfBoundsException("abc")))
    .cause<IndexOutOfBoundsException>() messageToContain "b"
//     | subject is now of type IndexOutOfBoundsException

fails {
    expect(IllegalStateException(IndexOutOfBoundsException("abc")))
        .cause<IllegalStateException>() messageToContain "d" // not shown in reporting as `cause<IllegalStateException>()` already fails

}

Return
The newly created Expect for the property Throwable.cause of the subject of this expectation

Since
0.12.0

inline infix fun <reified TExpected : Throwable> Expect<out Throwable>.cause(noinline assertionCreator: Expect<TExpected>.() -> Unit): Expect<TExpected> (source)

Expects that the property Throwable.cause of the subject is a TExpected (the same type or a sub-type) and holds all assertions the given assertionCreator creates for it.

Notice, in contrast to other assertion functions which expect an assertionCreator, this function returns not Expect of the initial type, which was some type T , but an Expect of the specified type TExpected.

expect(IllegalStateException(IndexOutOfBoundsException("abc")))
    .cause<IndexOutOfBoundsException> { // subject is now of type IndexOutOfBoundsException
        it messageToContain "b"
    }

fails { // because wrong type expected (IllegalStateException instead of IndexOutOfBoundsException), but since we use a block...
    expect(IllegalStateException(IndexOutOfBoundsException("abc"))).cause<IllegalStateException> {
        it messageToContain "b" // ... reporting mentions that subject's message was expected `to contain: "b"`
    }
}

fails {
    // because you forgot to define an expectation in the expectation group block
    // use `.cause<...>()` if this is all you expect
    expect(IllegalStateException(IndexOutOfBoundsException("abc"))).cause<IllegalStateException> { }
}

Return
an Expect for the subject of this expectation.

Since
0.12.0