Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.juandgaines.testground.domain.Place
import com.juandgaines.testground.domain.Profile
import com.juandgaines.testground.domain.User
import com.juandgaines.testground.domain.UserRepository
import com.juandgaines.testground.util.MainDispatcherRule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
Expand All @@ -24,14 +25,15 @@ import java.util.UUID
@OptIn(ExperimentalCoroutinesApi::class)
class ProfileViewModelTest {

private val testDispatcher = StandardTestDispatcher()
@get:Rule
val testDispatcher = MainDispatcherRule()

private lateinit var viewModel: ProfileViewModel
private lateinit var repository: UserRepositoryFake

@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)


repository = UserRepositoryFake()
viewModel = ProfileViewModel(
Expand All @@ -44,11 +46,6 @@ class ProfileViewModelTest {
)
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun givenValidUserId_whenLoadProfile_thenProfileIsLoaded() = runTest {
// Act
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.juandgaines.testground.util

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description) {
Dispatchers.resetMain()
}
}