notToThrow
Expects that no Throwable is thrown at all when calling the subject (a lambda with arity 0, i.e. without arguments) and changes the subject of this
expectation to the return value of type R.
Return
Samples
expect { "abc" }
.notToThrow() // subject is now of type String
.toContain("abc")
fails {
expect { throw IllegalStateException("abc") }
.notToThrow()
}
fun <R, T : () -> R> Expect<T>.notToThrow(assertionCreator: Expect<R>.() -> Unit): Expect<R>(source)
Expects that no Throwable is thrown at all when calling the subject (a lambda with arity 0, i.e. without arguments) and that the corresponding return value holds all assertions the given assertionCreator creates.
Return
Samples
expect { "abc" }
.notToThrow { // subject is now of type String
toEqual("abc")
} // subject keeps type String also after the block
fails { // because an exception was thrown, but since we use an expectation-group...
expect<() -> String> { throw IllegalStateException("abc") }
.notToThrow {
toStartWith("abc") // ... reporting mentions that subject's message was expected `to start with: "abc"`
}
}
fails { // because an exception was thrown, but since we use an expectation-group...
expect<() -> String> { throw IllegalStateException("abc") }
.notToThrow {
toStartWith("abc") // ... reporting mentions that subject's message was expected `to start with: "abc"`
}
}
fails {
// because you forgot to define an expectation in the expectation-group block
// use `.notToThrow()` if this is all you expect
expect { "abc" }.notToThrow {}
}