Привет, я новичок в тестировании, и у меня возникают проблемы при написании модульного теста для кода ниже
1.ViewModel
interface UserLocationViewModelInputs {
fun searchLocation(location: String)
}
interface UserLocationViewModelOutputs {
fun fetchingLocationSuggestions(): LiveData<Any>
fun locationSuggestion(): LiveData<List<SuggestedLocation>>
fun errorWhileFetchingLocationSuggestions(): LiveData<String>
}
class UserLocationViewModel @Inject constructor(private val findLocationSuggestionUseCase: FindLocationSuggestionUseCase) : ViewModel(), UserLocationViewModelInputs, UserLocationViewModelOutputs {
val inputs = this as UserLocationViewModelInputs
val outputs = this as UserLocationViewModelOutputs
// ##
// ## Fetching Location Suggestions
// ##
private val fetchingLocationSuggestions = MutableLiveData<Any>()
private val locationSuggestion = MutableLiveData<List<SuggestedLocation>>()
private val errorWhileFetchingLocationSuggestions = MutableLiveData<String>()
override fun fetchingLocationSuggestions(): LiveData<Any> {
return fetchingLocationSuggestions
}
override fun locationSuggestion(): LiveData<List<SuggestedLocation>> {
return locationSuggestion
}
override fun errorWhileFetchingLocationSuggestions(): LiveData<String> {
return errorWhileFetchingLocationSuggestions
}
override fun searchLocation(location: String) {
fetchingLocationSuggestions.postValue("fetching suggestions")
findLocationSuggestionUseCase.execute(LocationSuggestionSearchObserver(), location)
}
inner class LocationSuggestionSearchObserver : DisposableObserver<List<SuggestedLocation>>() {
override fun onComplete() {}
override fun onNext(t: List<SuggestedLocation>) {
locationSuggestion.postValue(t)
}
override fun onError(e: Throwable) {
errorWhileFetchingLocationSuggestions.postValue(e.message)
}
}
}
2.Вариант использования
class FindLocationSuggestionUseCase @Inject constructor(
private val locationRepository: LocationRepository
, threadExecutor: ThreadExecutor
, postExecutionThread: PostExecutionThread) : ObservableUseCase<String, List<SuggestedLocation>>(threadExecutor, postExecutionThread) {
override fun buildUseCaseObservable(params: String): Observable<List<SuggestedLocation>> {
return locationRepository.getLocationSuggestions(params)
}
}
3.Базовый вариант использования
abstract class ObservableUseCase<Params,ResponseType> internal constructor(
private val threadExecutor: ThreadExecutor,
private val postExecutionThread: PostExecutionThread) : UseCase {
private val disposables = CompositeDisposable()
/**
* Builds an [Observable] which will be used when executing the current [ObservableUseCase].
*/
internal abstract fun buildUseCaseObservable(params: Params): Observable<ResponseType>
/**
* Executes the current use case.
*
* @param observer [DisposableObserver] which will be listening to the observable build
* by [.buildUseCaseObservable] ()} method.
* @param params Parameters (Optional) used to build/execute this use case.
*/
fun execute(observer: DisposableObserver<ResponseType>, params: Params) {
val observable = this.buildUseCaseObservable(params)
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.scheduler)
addDisposable(observable.subscribeWith(observer))
}
/**
* Dispose from current [CompositeDisposable].
*/
fun dispose() {
if (disposables.isDisposed.not()) {
disposables.dispose()
}
}
/**
* Dispose from current [CompositeDisposable].
*/
private fun addDisposable(disposable: Disposable) {
disposables.add(disposable)
}
}
Я хочу проверить выборку Функция предложения местоположения - это мой модульный тест, я не могу понять, как проверить, вызван ли метод execute
для findLocationSuggestionUseCase
илинет, и как мне отправить фальшивый список предложений о местоположении обратно на viewmodel
Test
class LocationViewModelTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var viewModel: UserLocationViewModel
@Mock
lateinit var findLocationSuggestionUseCase: FindLocationSuggestionUseCase
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
this.viewModel = UserLocationViewModel(
this.findLocationSuggestionUseCase
)
}
@Test
fun searchLocationsSuccessTest() {
viewModel.inputs.searchLocation("Test")
Assert.assertTrue(viewModel.outputs.fetchingLocationSuggestions().value!!.equals("fetching suggestions"))
//Here I wanna test that execute method of findLocationSuggestionUseCase is called or not
//then I want to return Fake List of Location Suggestions
//then I want to test that fake list of Location Suggestions reached the view
}
}