android - 在为此代码编写单元测试时需要帮助
问题描述
嗨,我是测试新手,在为下面的代码编写单元测试时遇到了麻烦
1.视图模型
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
测试
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
}
}
解决方案
推荐阅读
- javascript - 如何在 setState 回调中调用 redux 操作?
- javascript - 如何在俄罗斯方块游戏中正确使用 requestAnimationFrame 函数?
- ruby-on-rails - 没有路线匹配 [GET] "/simulations"
- vue.js - 登录方法在推送路由之前不等待调度动作完成
- java - 一个组件需要一个名为“mongoTemplate”的 bean,在 JUnit 5 控制器测试中找不到该 bean
- python - tkinter 故障中的 GIF
- python - 如何将操作节点的输出保存在 Tensorflow 训练的图中?
- r - 如何读取数据
- talos - Talos --> TypeError: __init__() 得到了一个意外的关键字参数“grid_downsample”
- javascript - 递归正在跳过值