测试会使另一个失败

时间:2018-12-31 21:43:40

标签: android testing kotlin mockito

如果我分别运行每个测试,则它们将通过。如果我一起运行它们,则只有第一个通过(onGetAmountSuccess)。其他的则显示错误消息:想要但未被调用: view.showErrorMessage();

我已经尝试使用Mockito.reset,但是没有用。

 class Test {

private  var myRepository: MyRepository = mock()
private  var view : MyContract.View = mock()
private  var presenter : Presenter =  MyPresenter(myRepository)

@Before
fun setup() {
    RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
    presenter.attach(view)
}


@Test
fun onGetAmountSuccess(){
    doAnswer { Single.just(Currency("EUR", "12/31/2018", arrayListOf()))}.whenever(myRepository).getRates()
    presenter.getRates()
    verify(view, times(1)).updateAmount(arrayListOf)
}

@Test
fun onGetAmountFailure(){
    doAnswer { Single.error<Currency>(Exception("Error"))}.whenever(myRepository).getRates()
    presenter.getRates()
    verify(view, times(1)).showErrorMessage()
}

@Test
fun onUpdateAmountFailure(){
    doAnswer { Single.error<Currency>(Exception("Error"))}.whenever(myRepository).updateAmount("USD", 100.toBigDecimal())
    presenter.updateAmount("USD", 100.toBigDecimal())
    verify(view, times(1)).showErrorMessage()

}

}

演示者:

 override fun updateAmount(base : String, amount: BigDecimal) {
    myRepository.
        updateAmount(base,  amount)
        .compose(applySingleSchedulers())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe (
            {currency ->
                view?.updateAmount(amount)

            },
            {
                view?.showErrorMessage()
            }
        ).apply {
            disposables.add(this)
        }
}


   override fun getRates() {
    myRepository.getRates()
        .compose(applySingleSchedulers())
        .observeOn(AndroidSchedulers.mainThread())
         .repeatWhen{ done -> done.delay(1, TimeUnit.SECONDS) }
        .subscribe (
            {currency ->
                view?.updateAmount(amount)

            },
    {
        view?.showErrorMessage()
    }
    ).apply {
            disposables.add(this)
        }
}

我也尝试过使用模仿重置,但这没有用:

@After
fun reset(){
    Mockito.reset(view)
    Mockito.reset(myRepository)
    presenter = MyPresenter(myRepository)
    presenter.attach(view)

}

我正在使用以下lib:

com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0

有人知道我在做什么错以及如何解决吗?谢谢!

0 个答案:

没有答案