我正在编写单元测试,当我在执行挂起函数之前和之后立即更改可观察值时,我遇到了这种特殊情况。我想编写一个单元测试来检查该值是否正确更改了两次。
这是方法:
fun doSomething() {
viewModelScope.launch(ioDispatcher) {
try {
viewModelScope.launch(Dispatchers.Main) {
commandLiveData.value = Pair(CANCEL_TIMER, null)
}
isProgress.set(true) //need to test it was set to true in one test
checkoutInteractor.doSomethingElse().run {
handleResult(this)
}
isProgress.set(false) //need to test it was set to false here in another test
} catch (ex: java.lang.Exception) {
handleHttpError(ex)
isProgress.set(false)
}
}
}
编写测试时,我调用doSomething()
但我不确定如何检测该值在checkoutInteractor.doSomethingElse
调用之前设置为 true,在调用之后设置为 false。
这是我的测试
@Test
fun `test doSomething enables progress`() {
runBlockingTest {
doReturn(Response()).`when`(checkoutInteractor). checkoutInteractor.doSomethingElse()
viewModel.doSomething()
assertTrue(viewModel.isProgress.get()) //fails obviously as the value was already set to false after the `doSomethingElse` was executed.
}
}
顺便说一句,isProgress
是一个ObservableBoolean
您需要模拟或监视isProgress
和checkoutInteractor
字段,以记录和验证其方法的执行。
- 通过模拟或间谍
isProcess
并checkoutInteractor
进入您的班级。 - 执行
doSomething()
- 验证
set()
和doSomethingElse()
功能的顺序
例:
@Test
fun `test doSomething enables progress`() {
runBlockingTest {
val checkoutInteractor = Mockito.spy(CheckoutInteractor())
val isProcess = Mockito.spy(ObservableBoolean(true))
val viewModel = ViewModel(isProcess, checkoutInteractor)
viewModel.doSomething()
val inOrder = inOrder(isProcess, checkoutInteractor)
inOrder.verify(isProcess).set(true)
inOrder.verify(checkoutInteractor).doSomethingElse()
inOrder.verify(isProcess).set(false)
}
}