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

getExisting

infix 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 toEqual "a"
//                      | subject is of type String (actually "a")

fails {
    // fails because "a" is not equal to "b"
    expect(mapOf(1 to "a")) getExisting 1 toEqual "b"
    //                      | subject is of type String (actually "a")
}

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

Return
The newly created Expect for the extracted feature.

infix fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: KeyWithCreator<K, V>): 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 KeyWithCreator.assertionCreator creates for it.

expect(mapOf(1 to "a")) getExisting key(1) {   // subject inside this block is of type String (actually "a")
    this toEqual "a"
}

fails {
    expect(mapOf(1 to "a")) getExisting key(1) {  // subject inside this block is of type String (actually "a")
        this toEqual "b"   // fails because "a" is not equal to "b"
    }
}

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 key(2) {   // fails because key 2 does not exist
        this toEqual "a"    // still evaluated because we use an expectation group block
        // use `.getExisting(key).` if you want a fail fast behaviour
    }
}

Parameters

key - Use the function key(...) { ... } to create a KeyWithCreator where the first parameter corresponds to the key and the second is the assertionCreator-lambda

Return
an Expect for the subject of this expectation.