MainDispatcherFixture

class MainDispatcherFixture(val dispatcher: TestDispatcher = StandardTestDispatcher())(source)

Replaces Dispatchers.Main with dispatcher between install and reset, from shared code.

This is the multiplatform counterpart of the JUnit MainDispatcherRule, for tests in a commonTest source set that run on iOS as well as the JVM. Pair the two calls with kotlin.test's @BeforeTest and @AfterTest:

private val main = MainDispatcherFixture()

@BeforeTest fun setUp() = main.install()

@AfterTest fun tearDown() = main.reset()

@Test fun loads() = runTest(main.dispatcher) { ... }

Prefer runTestWithMain when one test body is all that needs the dispatcher.

Constructors

Link copied to clipboard
constructor(dispatcher: TestDispatcher = StandardTestDispatcher())

Properties

Link copied to clipboard
val dispatcher: TestDispatcher

The dispatcher installed as Dispatchers.Main.

Functions

Link copied to clipboard
fun install()

Installs dispatcher as Dispatchers.Main.

Link copied to clipboard
fun reset()

Restores the platform's Dispatchers.Main. Safe to call when install was not called.