首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spek + Retrofit测试崩溃

Spek + Retrofit测试崩溃
EN

Stack Overflow用户
提问于 2018-04-26 13:55:28
回答 2查看 239关注 0票数 1

我试着用Spek测试Retrofit

它将nullPointerException抛到{.}块上的

关联堆栈跟踪:https://pastebin.com/gy6dLtGg

这是我的考试课

代码语言:javascript
复制
@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课

代码语言:javascript
复制
 class RxSchedulersOverrideSpek : Spek({

    beforeGroup {
        RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
        RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
        RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
    }
})
EN

回答 2

Stack Overflow用户

发布于 2018-04-27 02:01:06

您应该使用memoized正确地设置测试值。问题是,accountCheckViewModel是在Spek的发现阶段初始化的,传递给accountCheckViewModelwebService模拟就是那个时候的值(您没有模拟它的任何方法)。beforeEachTest是在执行阶段运行的,您已经将webService重新分配到适当的模拟,但是accountCheckViewModel仍然保存前面的值。

代码语言:javascript
复制
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)
    }
  }
}
票数 1
EN

Stack Overflow用户

发布于 2018-05-02 20:25:42

假设您使用的是RxJava2 RxAndroid ,则应该使用Schedulers.trampoline()覆盖RxAndroid调度程序。这样,在蹦床()上订阅的所有作业都将在同一个线程中依次排队和执行。

代码语言:javascript
复制
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }

您的RxSchedulersOverrideSpek.kt应该如下所示:

代码语言:javascript
复制
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()
    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50044879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档