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.
import io.mockk.every
import io.mockk.mockk
every { navigator.currentLocation } returns "Home"
every { navigator.navigateTo("Park") } throws IllegalStateException("Can't reach the park")
// 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.