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.

Return

The newly created Expect for the extracted feature.

Samples

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
}

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.

Return

an Expect for the subject of this expectation.

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

Samples

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

fails { // because key 2 does not exist but...

    expect(mapOf(1 to "a")) getExisting key(2) {
        this toEqual "a"    //... still reported as we are inside an expectation-group block
    }
}

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

    expect(mapOf(1 to "a")) getExisting key(1) {
        it toEqual "b"   // fails because "a" is not equal to "b"
        it toEqual "c"   // still evaluated, use `getExisting 1` if you want a fail fast behaviour
    }
}