我试图测试一个演示者,但测试只在事情发生在Observable之外时才传递,我的演示者:
class CompManCodePresenter @Inject constructor(val apiBag: ServiceModule.ApiBag, disposable: CompositeDisposable, scheduler: SchedulerProvider) : BasePresenter<CompManCodeView>(disposable, scheduler) {
fun validateCompmanCode(code: String) {
view?.showProgress()
if (code.isEmpty()) {
view?.emptyCode()
view?.hideProgress()
} else {
val request = mutableMapOf<String, Any?>()
request.put("code", code)
disposable.add(
apiBag.apiMainFast.check_code(JWT.jwt_encrypted(request))
.subscribeOn(scheduler.io())
.observeOn(scheduler.ui())
.subscribe(
{ result ->
view?.hideProgress()
},
{ error ->
view?.hideProgress()
view?.onError()
})
)
}
}
休息api部分:
@POST("v2/check-code")
fun check_code(@Body body: JwtDataRequest): Observable<SyncCompManAndDeviceResponse>
测试工作正常:
@Test
fun test_code_empty() {
val mockedResponse: Throwable = mock()
val params = HashMap<String, Any>()
val mockedRequest = JWT.jwt_encrypted(params)
doReturn(Observable.just(mockedResponse)).`when`(api).check_code(mockedRequest)
presenter.validateCompmanCode("")
testScheduler.triggerActions()
verify(view).showProgress()
verify(view).emptyCode()
verify(view).hideProgress()
}
测试不起作用(它只在外面运行.showProgress
@Test
fun test_no_connection() {
val mockedResponse: Throwable = mock()
val params = HashMap<String, Any>()
val mockedRequest = JWT.jwt_encrypted(params)
doReturn(Observable.just(mockedResponse)).`when`(api).check_code(mockedRequest)
presenter.validateCompmanCode("A")
testScheduler.triggerActions()
verify(view).showProgress()
verify(view).hideProgress()
verify(view).onError()
}
我的测试类设置:
private val view: CompManCodeView = mock()
private lateinit var presenter: CompManCodePresenter
private lateinit var testScheduler: TestScheduler
val api:RestAPI = Mockito.mock(RestAPI::class.java, Mockito.RETURNS_DEEP_STUBS)
val apiBag:ServiceModule.ApiBag = Mockito.spy(ServiceModule.ApiBag(api,api,api,api))
@Before
fun setUp() {
MockitoAnnotations.initMocks(this);
val compositeDisposable = CompositeDisposable()
testScheduler = TestScheduler()
val testSchedulerProvider = TestSchedulerProvider(testScheduler)
presenter = CompManCodePresenter(apiBag, disposable = compositeDisposable, scheduler = testSchedulerProvider)
presenter.attachView(view)
}
没有错误显示,测试结果显示的唯一内容是:
Wanted but not invoked:
view.hideProgress();
However, there were exactly 2 interactions with this mock:
view.setPresenter(
CompManCodePresenter@72ee5d84
);
-> BasePresenter.attachView(BasePresenter.kt:13)
view.showProgress();
-> at CompManCodePresenter.validateCompmanCode(CompManCodePresenter.kt:21)
我是这样的:https://github.com/burakeregar/KotlinRxMvpArchitecture
答案 0 :(得分:0)
我找到了答案,我不知道为什么,我之前测试了这个,但却抛出了关于对象不是模拟的错误,但我再次测试并且工作了:
变化
val api:RestAPI = Mockito.mock(RestAPI::class.java, Mockito.RETURNS_DEEP_STUBS)
到
val api:RestAPI = Mockito.spy(MockRetrofitApi.mockApi())
mockApi只返回new Retrofit.create(RestApi.class)