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

时间:2019-03-23 12:24:37

标签: android unit-testing mockito

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

1。 ViewModel

oper = input("What operation: ")
calc = int(input("How many calculations: "))
tall1 = int(input("Type a number: "))
tall2 = int(input("Type another number: "))

if oper == ("+"):
    for num in range(calc):
        print(tall1 + tall2)
elif oper == ("*"):
        print(tall1 * tall2)
elif oper == ("/"):
        print(tall1 / tall2)
elif oper == ("-"):
    for num in range(calc):
        print(tall1 - tall2)

2。用例

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

    }
}

3。基本用例

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) } } 的{​​{1}}方法是否被调用以及如何发送伪造品回到execute

的位置建议列表

测试

findLocationSuggestionUseCase

0 个答案:

没有答案