Add Robolectric to allow ViewModel tests to pass

Change-Id: I782fab167103edbdaa1d95839b4fbc6e659fef4e
dt/nav-safe-args-android-dependency
Don Turner 4 months ago
parent 95bdc0f533
commit ec71b2a07e

@ -28,6 +28,7 @@ dependencies {
implementation(projects.core.domain)
testImplementation(projects.core.testing)
testImplementation(libs.robolectric)
androidTestImplementation(projects.core.testing)
}

@ -34,12 +34,21 @@ import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals
/**
* To learn more about how this test handles Flows created with stateIn, see
* https://developer.android.com/kotlin/flow/test#statein
*
* These tests use Robolectric because the subject under test (the ViewModel) uses
* `SavedStateHandle.toRoute` which has a dependency on `android.os.Bundle`.
*
* TODO: Remove Robolectric if/when AndroidX Navigation API is updated to remove Android dependency.
* See b/340966212.
*/
@RunWith(RobolectricTestRunner::class)
class InterestsViewModelTest {
@get:Rule
@ -56,8 +65,6 @@ class InterestsViewModelTest {
@Before
fun setup() {
viewModel = InterestsViewModel(
// TODO: This line causes tests to fail since it introduces an Android dependency
// see b/340966212 for more information
savedStateHandle = SavedStateHandle(
route = InterestsRoute(initialTopicId = testInputTopics[0].topic.id),
),

@ -28,6 +28,7 @@ dependencies {
implementation(projects.core.data)
testImplementation(projects.core.testing)
testImplementation(libs.robolectric)
androidTestImplementation(projects.core.testing)
}

@ -17,6 +17,7 @@
package com.google.samples.apps.nowinandroid.feature.topic
import androidx.lifecycle.SavedStateHandle
import androidx.navigation.testing.invoke
import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
@ -25,6 +26,7 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepo
import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import com.google.samples.apps.nowinandroid.feature.topic.navigation.TopicRoute
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
@ -35,13 +37,22 @@ import kotlinx.datetime.Instant
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals
import kotlin.test.assertIs
/**
* To learn more about how this test handles Flows created with stateIn, see
* https://developer.android.com/kotlin/flow/test#statein
*
* These tests use Robolectric because the subject under test (the ViewModel) uses
* `SavedStateHandle.toRoute` which has a dependency on `android.os.Bundle`.
*
* TODO: Remove Robolectric if/when AndroidX Navigation API is updated to remove Android dependency.
* * See b/340966212.
*/
@RunWith(RobolectricTestRunner::class)
class TopicViewModelTest {
@get:Rule
@ -59,8 +70,9 @@ class TopicViewModelTest {
@Before
fun setup() {
viewModel = TopicViewModel(
// TODO: Figure out how to supply the correct dependency TopicDestination(id = testInputTopics[0].topic.id)
savedStateHandle = SavedStateHandle(mapOf("id" to testInputTopics[0].topic.id)),
savedStateHandle = SavedStateHandle(
route = TopicRoute(id = testInputTopics[0].topic.id),
),
userDataRepository = userDataRepository,
topicsRepository = topicsRepository,
userNewsResourceRepository = userNewsResourceRepository,

Loading…
Cancel
Save