Skip to content

Testing StateFlow not get all states #3519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mustafatatarhan0 opened this issue Nov 3, 2022 · 2 comments
Closed

Testing StateFlow not get all states #3519

mustafatatarhan0 opened this issue Nov 3, 2022 · 2 comments
Labels

Comments

@mustafatatarhan0
Copy link

mustafatatarhan0 commented Nov 3, 2022

I tried to test StateFlows states. But I cannot get all states. Just last state what I getting. Here's my view model:

@HiltViewModel
class ExerciseViewModel @Inject constructor(
    private val exerciseRepository: BaseRepository,
    private val exerciseMapper: ExerciseMapper
) : ViewModel() {

    private val _data = MutableStateFlow(Exercise())
    val data: StateFlow<Exercise> = _data.asStateFlow()
    private val _state = MutableStateFlow<ExerciseViewState?>(null)
    val state: StateFlow<ExerciseViewState?> = _state.asStateFlow()

    fun loadExercise(id: Int) {
        viewModelScope.launch {
            try {
                _state.update { ExerciseViewState.Loading }
                val result = exerciseRepository.getExercise(id)
                _data.value = exerciseMapper.map(result)
                _state.value = ExerciseViewState.Data
            } catch (e: Exception) {
                _state.value = ExerciseViewState.Error
            }
        }
    }
}

And here's what I am trying to test:

@ExperimentalCoroutinesApi
class ExerciseViewModelTest {

    @get:Rule
    var rule = InstantTaskExecutorRule()

    @get:Rule
    val mainDispatcherRule = MainDispatcherRule()

    private var exerciseRepository = FakeExerciseRepository()
    private var exerciseMapper = ExerciseMapper()
    lateinit var viewModel: ExerciseViewModel

    @Before
    fun setUp() {
        MockKAnnotations.init(this)
        viewModel = ExerciseViewModel(
            exerciseRepository = exerciseRepository,
            exerciseMapper = exerciseMapper
        )
    }

    @Test
    fun loadExercise_ShouldSetLoadingState() = runTest {
        val stateList = mutableListOf<ExerciseViewState?>()
        val job = launch(UnconfinedTestDispatcher(testScheduler)) {
            viewModel.state.toList(stateList)
        }
        viewModel.loadExercise(id = 1)
        advanceUntilIdle()
        assertContains(stateList, ExerciseViewState.Loading)
        job.cancel()
    }
}

stateList returns only last state ([ExerciseViewState.Error]) I wanna reach all the values what states have. ([Loading,Data,Error])

@dkhalanskyjb
Copy link
Collaborator

Hi! This question was asked several times already: https://github.com/Kotlin/kotlinx.coroutines/issues?q=label%3Atest+StateFlow So, closing as duplicate. The discussion that is the simplest to follow is, in my opinion, this one: #3339

In general, for questions about usage, please use Stack Overflow or the Kotlin Slack channel.

@dkhalanskyjb dkhalanskyjb closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2022
@dkhalanskyjb
Copy link
Collaborator

The master issue for this topic is now #3939

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants