我正在使用 Kotlin、RxJava 和 Retrofit 开发一个 android 应用程序。
我正在发送删除资源的请求。
HTTP - 删除
响应是 204 No Content。我的改造代码如下:
@DELETE("res/{resId}")
fun deleteJob(@Path("resId") resId: String): Observable<Unit>
在这种情况下,我不知道如何定义返回类型。所以我定义了“可观察的”。因为没有响应体。响应代码为 204。
以下是我的演示者代码:
override fun deleteRes(resId: String) {
restService.deleteRes(resId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
// here is not called
}, {
// Always here is called, because the response is 204.
if (it is NoContentException) { // I defined new exception.
view.removeRes(resId)
} else {
Log.e(TAG, "deleteRes - failed: ${it.message}")
}
})
}
我想测试这个 Presenter 功能。
下面是我的测试代码:
@Test
fun deleteResTest() {
val deleteResId = "delete_res_id"
Mockito.`when`(mockRestService.deleteRes(deleteResId)).thenReturn(Observable.just(Unit))
mockRestService.deleteRes(deleteResId)
.toFlowable(BackpressureStrategy.BUFFER)
.subscribe(TestSubscriber.create<Unit>())
mJobsPresenter.deleteRes(deleteResId)
Mockito.verify(mockView).removeRes(deleteResId)
}
但是当我运行这个测试代码时,它失败了:
Wanted but not invoked:
view.removeRes("delete_res_id");
-> at com.RessPresenterTest.deleteResTest(ResPresenterTest.kt:95)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
view.removeRes("delete_res_id");
-> at com.RessPresenterTest.deleteResTest(ResPresenterTest.kt:95)
Actually, there were zero interactions with this mock.
at com.RessPresenterTest.deleteResTest(ResPresenterTest.kt:95)
有人帮帮我好吗?