我试着用Spek测试Retrofit
它将nullPointerException抛到{.}块上的上
关联堆栈跟踪:https://pastebin.com/gy6dLtGg
这是我的考试课
@RunWith(JUnitPlatform::class)
class AccountCheckViewModelTest : Spek({
include(RxSchedulersOverrideRule)
val httpException = mock<HttpException> {
on { code() }.thenReturn(400)
}
given(" account check view model") {
var accountCheckRequest = mock<CheckExistingAccountRequest>()
var accountCheckResponse = mock<CheckExistingAccountResponse>()
var webService = mock<IAPICalls>()
val accountCheckViewModel = spy(VMAccountCheck(webService))
beforeEachTest {
accountCheckRequest = mock<CheckExistingAccountRequest>() {
on { email }.thenReturn("foo@mail")
}
accountCheckResponse = mock<CheckExistingAccountResponse>() {
on { firstName }.thenReturn("foo")
on { email }.thenReturn("foo@mail")
}
webService = mock<IAPICalls> {
on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
}
}
on("api success") {
accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)
it("should call live data with first name as foo") {
verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
}
}
}
}这是我的RxSchedulersOverrideSpek课
class RxSchedulersOverrideSpek : Spek({
beforeGroup {
RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
}
})发布于 2018-04-27 02:01:06
您应该使用memoized正确地设置测试值。问题是,accountCheckViewModel是在Spek的发现阶段初始化的,传递给accountCheckViewModel的webService模拟就是那个时候的值(您没有模拟它的任何方法)。beforeEachTest是在执行阶段运行的,您已经将webService重新分配到适当的模拟,但是accountCheckViewModel仍然保存前面的值。
given(" account check view model") {
val accountCheckRequest by memoized {
mock<CheckExistingAccountRequest>() {
on { email }.thenReturn("foo@mail")
}
}
val accountCheckResponse by memoized {
mock<CheckExistingAccountResponse>() {
on { firstName }.thenReturn("foo")
on { email }.thenReturn("foo@mail")
}
}
val webService by memoized {
mock<IAPICalls> {
on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
}
}
val accountCheckViewModel by memoized {
spy(VMAccountCheck(webService))
}
on("api success") {
accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)
it("should call live data with first name as foo") {
verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
}
}
}发布于 2018-05-02 20:25:42
假设您使用的是RxJava2和 RxAndroid ,则应该使用Schedulers.trampoline()覆盖RxAndroid调度程序。这样,在蹦床()上订阅的所有作业都将在同一个线程中依次排队和执行。
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }您的RxSchedulersOverrideSpek.kt应该如下所示:
object RxSchedulersOverrideSpek : Spek({
beforeGroup {
RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
}
afterGroup {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
}
})https://stackoverflow.com/questions/50044879
复制相似问题