Stub out behaviour

    Stubbing allows you to setup canned answers when functions are called. You only need to stub methods that are called by your class, and you can ignore methods that aren’t run during your test.

    1. import io.mockk.every
    2. import io.mockk.mockk
    3. every { navigator.currentLocation } returns "Home"
    4. every { navigator.navigateTo("Park") } throws IllegalStateException("Can't reach the park")
    5. // throws an IllegalStateException

    In MockK, stubs are created by using the mockk and every functions. mockk creates your pretend object, and every lets you define canned answers for different functions on that pretend object. every starts a stubbing block and uses anonymous functions and infix functions to define the stub.

    To define what happens when the stubbed method is called, an answer function such as returns is used. returns "Home" tells MockK to always return the string "Home" when the currentLocation getter is called.