-
Notifications
You must be signed in to change notification settings - Fork 0
Add test to MovieViewModel #79
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package com.architectcoders.presentation.viewmodels | ||
|
|
||
| import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
| import androidx.lifecycle.Observer | ||
| import com.gabriel.usecases.GetFavoriteMoviesUseCase | ||
| import com.gabriel.usecases.GetPopularMoviesUseCase | ||
| import com.gabriel.usecases.GetSearchMoviesUseCase | ||
| import com.gabriel.usecases.GetTopRatedMoviesUseCase | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.Mock | ||
| import org.mockito.Mockito.* | ||
| import org.mockito.junit.MockitoJUnitRunner | ||
|
|
||
| @RunWith(MockitoJUnitRunner.Silent::class) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ¿Para qué sirve lo de
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hay un error de logs de Mokito sobre extra stubs. he buscado y dicen que es un error de Mokito, asi que con Silent silencias esos extra logs.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chachi. Gracias! |
||
| class MovieViewModelTest { | ||
|
|
||
| @get:Rule | ||
| val rule = InstantTaskExecutorRule() | ||
|
|
||
| @Mock | ||
| lateinit var getPopularMoviesUseCase: GetPopularMoviesUseCase | ||
|
|
||
| @Mock | ||
| lateinit var getTopRatedMoviesUseCase: GetTopRatedMoviesUseCase | ||
|
|
||
| @Mock | ||
| lateinit var getSearchMovieListUseCase: GetSearchMoviesUseCase | ||
|
|
||
| @Mock | ||
| lateinit var getFavoriteMoviesUseCase: GetFavoriteMoviesUseCase | ||
|
|
||
| @Mock | ||
| lateinit var observer: Observer<MovieViewModel.UiModel> | ||
|
|
||
| private lateinit var viewModel: MovieViewModel | ||
|
|
||
| @ExperimentalCoroutinesApi | ||
| @Before | ||
| fun setUp() { | ||
| viewModel = MovieViewModel( | ||
| getPopularMoviesUseCase, | ||
| getTopRatedMoviesUseCase, | ||
| getSearchMovieListUseCase, | ||
| getFavoriteMoviesUseCase, | ||
| Dispatchers.Unconfined | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `after Initialise it request movies`() = runBlocking { | ||
| viewModel.model.observeForever(observer) | ||
| verify(observer, times(1)).onChanged(MovieViewModel.UiModel.RequestMovies) | ||
| } | ||
|
|
||
| @Test | ||
| fun onRequestPopularMovies() = runBlocking { | ||
| viewModel.model.observeForever(observer) | ||
|
|
||
| `when`(viewModel.onRequestPopularMovies()).thenCallRealMethod() | ||
| verify(observer, times(1)).onChanged(MovieViewModel.UiModel.Loading) | ||
| verify(getPopularMoviesUseCase, times(0)).execute({ | ||
| viewModel.handleMoviesResponse(it) | ||
| },{ | ||
| viewModel.handleErrorResponse(it) | ||
| }) | ||
| } | ||
|
|
||
| @Test | ||
| fun onRequestTopRatedMovies() = runBlocking { | ||
| viewModel.model.observeForever(observer) | ||
|
|
||
| `when`(viewModel.onRequestTopRatedMovies()).thenCallRealMethod() | ||
| verify(observer, times(1)).onChanged(MovieViewModel.UiModel.Loading) | ||
| verify(getTopRatedMoviesUseCase, times(0)).execute({ | ||
| viewModel.handleMoviesResponse(it) | ||
| },{ | ||
| viewModel.handleErrorResponse(it) | ||
| }) | ||
| } | ||
|
|
||
| @Test | ||
| fun onRequestFavoriteMovies() = runBlocking { | ||
| viewModel.model.observeForever(observer) | ||
|
|
||
| `when`(viewModel.onRequestFavoriteMovies()).thenCallRealMethod() | ||
| verify(observer, times(1)).onChanged(MovieViewModel.UiModel.Loading) | ||
| verify(getPopularMoviesUseCase, times(0)).execute({ | ||
| viewModel.handleMoviesResponse(it) | ||
| },{ | ||
| viewModel.handleErrorResponse(it) | ||
| }) | ||
| } | ||
|
|
||
| @Test | ||
| fun onSearchMovies() = runBlocking { | ||
| viewModel.model.observeForever(observer) | ||
|
|
||
| val query = "001" | ||
| `when`(viewModel.onSearchMovies(query)).thenCallRealMethod() | ||
| verify(observer, times(1)).onChanged(MovieViewModel.UiModel.Loading) | ||
| verify(getSearchMovieListUseCase, times(0)).execute({ | ||
| viewModel.handleMoviesResponse(it) | ||
| },{ | ||
| viewModel.handleErrorResponse(it) | ||
| }, | ||
| query | ||
| ) | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No conocía esto... mola!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jajaja, ya te digo, yo creo que es lo que mas uso en testing.