toBeAFailure

Deprecated

The whole kotlin_1_3 extension will be removed with 1.2.0, use the function from the ch.tutteli.atrium.api.fluent.en_GB package

Replace with

import ch.tutteli.atrium.api.fluent.en_GB.toBeAFailure
this.toBeAFailure<TExpected>()

Expects that the subject of this expectation (a Result) is a failure (Result.isFailure) and that it encapsulates an exception of type TExpected.

Return

An Expect with the new type TExpected

Since

0.17.0

Samples

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.fluent.en_GB.toBeLessThan
import ch.tutteli.atrium.api.fluent.en_GB.toBeGreaterThan
import ch.tutteli.atrium.api.fluent.en_GB.messageToContain
import ch.tutteli.atrium.api.fluent.en_GB.message
import ch.tutteli.atrium.api.fluent.en_GB.toStartWith
import ch.tutteli.atrium.api.fluent.en_GB.kotlin_1_3.toBeAFailure
import ch.tutteli.atrium.api.fluent.en_GB.kotlin_1_3.toBeASuccess
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
fun main() { 
   //sampleStart 
   val message = "wrong argument"
val failure = Result.failure<Int>(IllegalArgumentException(message))

expect(failure)
    .toBeAFailure<IllegalArgumentException>() // subject is now of type IllegalArgumentException
    .messageToContain("argument")


fails { // because sub-expectation fails
    expect(failure)
        .toBeAFailure<IllegalArgumentException>()
        .messageToContain("parameter") // fails
}

fails { // because wrong Expectation type expected
    expect(failure)
        .toBeAFailure<ArithmeticException>() // fails
        .messageToContain("parameter")       // not evaluated/reported because toBeAFailure already fails
    //                                          use `toBeAFailure<...> { ... }` if you want that all expectations are evaluated
}

fails { // because it was a Success
    expect(Result.success(10))
        .toBeAFailure<IllegalArgumentException>() // fails
        .messageToContain("parameter")            // not evaluated/reported because toBeAFailure already fails
    //                                               use `toBeAFailure<...> { ... }` if you want that all expectations are evaluated
} 
   //sampleEnd
}

inline fun <TExpected : Throwable> Expect<out Result<*>>.toBeAFailure(noinline assertionCreator: Expect<TExpected>.() -> Unit): Expect<TExpected>(source)

Deprecated

The whole kotlin_1_3 extension will be removed with 1.2.0, use the function from the ch.tutteli.atrium.api.fluent.en_GB package

Replace with

import ch.tutteli.atrium.api.fluent.en_GB.toBeAFailure
this.toBeAFailure<TExpected>(assertionCreator)

Expects that the subject of this expectation (a Result) is a failure (Result.isFailure) , that it encapsulates an exception of type TExpected and that the exception holds all assertions the given assertionCreator creates.

Return

An Expect with the new type TExpected

Since

0.17.0

Samples

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.fluent.en_GB.toBeLessThan
import ch.tutteli.atrium.api.fluent.en_GB.toBeGreaterThan
import ch.tutteli.atrium.api.fluent.en_GB.messageToContain
import ch.tutteli.atrium.api.fluent.en_GB.message
import ch.tutteli.atrium.api.fluent.en_GB.toStartWith
import ch.tutteli.atrium.api.fluent.en_GB.kotlin_1_3.toBeAFailure
import ch.tutteli.atrium.api.fluent.en_GB.kotlin_1_3.toBeASuccess
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
fun main() { 
   //sampleStart 
   val errorMessage = "can not divide by zero"
val failure = Result.failure<Int>(ArithmeticException(errorMessage))

expect(failure).toBeAFailure<ArithmeticException> {  // subject within this expectation-group is of type ArithmeticException
    messageToContain("by zero")
} // subject here is back to type Result<Int>

fails { // because sub-expectation fails
    expect(failure).toBeAFailure<IllegalArgumentException> {
        messageToContain("parameter") // fails
        message.toStartWith("wrong")  // still evaluated even though messageToContain already fails
        //                               use `.toBeAFailure.` if you want a fail fast behaviour
    }
}

fails { // because wrong Expectation type expected, but since we use an expectation-group...
    expect(failure).toBeAFailure<ArithmeticException> {
        messageToContain("parameter") // ...reporting mentions that subject's message was expected `to contain: "parameter"``
        //                               use `.toBeAFailure.` if you want a fail fast behaviour
    }
}

fails { // because it was a Success, but since we use a block
    expect(Result.success(10)).toBeAFailure<IllegalArgumentException> {
        messageToContain("parameter") // ...reporting mentions that subject's message was expected `to contain: "parameter"``
        //                               use `.toBeAFailure.` if you want a fail fast behaviour
    }
} 
   //sampleEnd
}