We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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])
The text was updated successfully, but these errors were encountered:
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.
Sorry, something went wrong.
The master issue for this topic is now #3939
No branches or pull requests
I tried to test StateFlows states. But I cannot get all states. Just last state what I getting. Here's my view model:
And here's what I am trying to test:
stateList returns only last state ([ExerciseViewState.Error]) I wanna reach all the values what states have. ([Loading,Data,Error])
The text was updated successfully, but these errors were encountered: