From 95250dccc2d25b027e32bd637cdb39340870ad13 Mon Sep 17 00:00:00 2001 From: Juan Gaines Date: Tue, 8 Apr 2025 20:57:56 -0500 Subject: [PATCH 1/3] TestDispatchers --- .../presentation/ProfileViewModelTest.kt | 15 ++------------- .../testground/util/MainDispatcherRule.kt | 3 ++- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt b/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt index 2d96c49..08fb991 100644 --- a/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt +++ b/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt @@ -9,14 +9,9 @@ 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 import kotlinx.coroutines.test.advanceUntilIdle -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain -import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test @@ -25,15 +20,14 @@ import java.util.UUID @OptIn(ExperimentalCoroutinesApi::class) class ProfileViewModelTest { - private val testDispatcher = StandardTestDispatcher() + @get:Rule + val mainDispatcherRule = MainDispatcherRule() private lateinit var viewModel: ProfileViewModel private lateinit var repository: UserRepositoryFake @Before fun setUp() { - Dispatchers.setMain(testDispatcher) - repository = UserRepositoryFake() viewModel = ProfileViewModel( repository = repository, @@ -45,11 +39,6 @@ class ProfileViewModelTest { ) } - @After - fun tearDown() { - Dispatchers.resetMain() - } - @Test fun givenValidUserId_whenLoadProfile_thenProfileIsLoaded() = runTest { // Act diff --git a/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt b/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt index d4689ee..82bffd7 100644 --- a/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt +++ b/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt @@ -2,6 +2,7 @@ package com.juandgaines.testground.util import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestDispatcher import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain @@ -11,7 +12,7 @@ import org.junit.runner.Description @OptIn(ExperimentalCoroutinesApi::class) class MainDispatcherRule( - private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher() + private val testDispatcher: TestDispatcher = StandardTestDispatcher() ) : TestWatcher() { override fun starting(description: Description) { Dispatchers.setMain(testDispatcher) From e81ae116f0422d2fee04479c925a70a01af69206 Mon Sep 17 00:00:00 2001 From: Juan Gaines Date: Tue, 8 Apr 2025 21:00:14 -0500 Subject: [PATCH 2/3] TestDispatchers --- .../presentation/ProfileViewModelTest.kt | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt b/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt index 08fb991..8a1eecc 100644 --- a/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt +++ b/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt @@ -65,22 +65,6 @@ class ProfileViewModelTest { assertThat(viewModel.state.value.isLoading).isFalse() } - @Test - fun givenLoadingState_whenLoadProfile_thenStateUpdatesCorrectly() = runTest { - // Act & Assert - viewModel.state.test { - val emission1 = awaitItem() - assertThat(emission1.isLoading).isFalse() - - viewModel.loadProfile() - val emission2 = awaitItem() - assertThat(emission2.isLoading).isTrue() - - val emission3 = awaitItem() - assertThat(emission3.isLoading).isFalse() - assertThat(emission3.profile).isEqualTo(repository.profileToReturn) - } - } } From 0bf00635e51238933e94309f066eaa7ff9d80e77 Mon Sep 17 00:00:00 2001 From: Juan Gaines Date: Thu, 10 Apr 2025 11:59:12 -0500 Subject: [PATCH 3/3] navigation tests --- .../presentation/ProfileViewModelTest.kt | 11 ++++----- .../testground/util/MainDispatcherRule.kt | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt diff --git a/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt b/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt index 4c0f5d5..322cea0 100644 --- a/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt +++ b/app/src/test/java/com/juandgaines/testground/presentation/ProfileViewModelTest.kt @@ -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 @@ -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( @@ -44,11 +46,6 @@ class ProfileViewModelTest { ) } - @After - fun tearDown() { - Dispatchers.resetMain() - } - @Test fun givenValidUserId_whenLoadProfile_thenProfileIsLoaded() = runTest { // Act diff --git a/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt b/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt new file mode 100644 index 0000000..9447bac --- /dev/null +++ b/app/src/test/java/com/juandgaines/testground/util/MainDispatcherRule.kt @@ -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() + } +} \ No newline at end of file