doc / ch.tutteli.atrium.api.fluent.en_GB / getExisting

getExisting

fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K): Expect<V> (source)

Expects that the subject of this expectation (a Map) contains the given key, creates an Expect for the corresponding value and returns the newly created Expect, so that further fluent calls are expectations about it.

expect(mapOf(1 to "a"))
    .getExisting(1) // subject is of type String (actually "a")
    .toEqual("a")

fails {
    expect(mapOf(1 to "a"))
        .getExisting(1)  // subject is of type String (actually "a")
        .toContain("b")    // fails
        .toStartWith("z")  // not
}

fails {
    expect(mapOf(1 to "a"))
        .getExisting(2) // expectation fails because key 2 does not exist
        .toEqual("a")   // not evaluated/reported even though `getExisting` already fails
    //                     use `.getExisting(key) { ... }` if you want that all expectations are evaluated
}

Return
The newly created Expect for the extracted feature.

fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K, assertionCreator: Expect<V>.() -> Unit): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains the given key and that the corresponding value holds all assertions the given assertionCreator creates for it.

expect(mapOf(1 to "a"))
    .getExisting(1) {   // subject inside this block is of type String (actually "a")
        toEqual("a")
    } // subject here is back to type Map<Int, String>

fails {
    expect(mapOf(1 to "a"))
        .getExisting(1) {    // subject inside this block is of type String (actually "a")
            toEqual("b")     // fails because "a" is not equal to "b"
            toStartWith("z") // still evaluated because we use an expectation group block
            //                  use `.getExisting(key).` if you want a fail fast behaviour
        }
}

fails {
    // all expectations are evaluated inside an expectation group block; for more details:
    // https://github.com/robstoll/atrium#define-single-assertions-or-assertion-groups

    expect(mapOf(1 to "a"))
        .getExisting(2) {   // fails because key 2 does not exist
            toEqual("a")    // still evaluated because we use an expectation group block
            //                 use `.getExisting(key).` if you want a fail fast behaviour
        }
}

Return
an Expect for the subject of this expectation.