how to make Unit Test
Paging3
Kotlin
in my file project test case MovieViewModelTest.kt, I don't know how implement it?
MovieViewModelTest.kt
@RunWith(MockitoJUnitRunner::class)
class MovieViewModelTest {
private lateinit var viewModel: MovieViewModel
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Mock
private lateinit var filmRepository: FilmRepository
@Mock
private lateinit var savedStateHandle: SavedStateHandle
@Mock
private lateinit var observer: Observer<in PagingData<Film>>
@Mock
private lateinit var pagingData: PagingData<Film>
@Before
fun setUp() {
viewModel = MovieViewModel(filmRepository, savedStateHandle)
}
@Test
fun getListMovie() {
//??how implement unit test paging 3 here??
......
}
}
CodePudding user response:
This from my project MovieViewModel.kt
class MovieViewModel @ViewModelInject constructor(
repository: FilmRepository,
@Assisted state: SavedStateHandle
) : ViewModel() {
private val currentQuery = state.getLiveData(CURRENT_QUERY, EMPTY_QUERY)
val movies = currentQuery.switchMap { query ->
if (query.isNotEmpty()) {
repository.getSearchMovies(query).cachedIn(viewModelScope)
} else {
repository.getNowPlaying().cachedIn(viewModelScope)
}
}
fun searchMovies(query: String) {
currentQuery.value = query
}
companion object {
private const val CURRENT_QUERY = "current_query"
private const val EMPTY_QUERY = ""
}
}
CodePudding user response:
This from my project FilmRepository.kt
@Singleton
class FilmRepository @Inject constructor(private val apiService: ApiService) {
private val remoteDataSource = RemoteDataSource
fun getNowPlaying() =
Pager(
config = PagingConfig(
pageSize = 5,
maxSize = 20,
enablePlaceholders = false
),
pagingSourceFactory = { MoviePagingSource(apiService, null) }
).liveData
fun getSearchMovies(query: String) =
Pager(
config = PagingConfig(
pageSize = 5,
maxSize = 20,
enablePlaceholders = false
),
pagingSourceFactory = { MoviePagingSource(apiService, query) }
).liveData
}