Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 需要帮助编写此代码的单元测试吗_Android_Unit Testing_Mockito - Fatal编程技术网

Android 需要帮助编写此代码的单元测试吗

Android 需要帮助编写此代码的单元测试吗,android,unit-testing,mockito,Android,Unit Testing,Mockito,嗨,我是测试新手,在为下面的代码编写单元测试时遇到了麻烦 1。ViewModel interface UserLocationViewModelInputs { fun searchLocation(location: String) } interface UserLocationViewModelOutputs { fun fetchingLocationSuggestions(): LiveData<Any> fun locationSuggesti

嗨,我是测试新手,在为下面的代码编写单元测试时遇到了麻烦

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)
        }

    }
}
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)
    }

}
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)
    }
}
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 
    }


}