这就是模拟的用途。您应该只测试 ViewModel 的逻辑,而忽略 Managers/Helpers 内部的逻辑。其他一切都应该被嘲笑/伪造:
@Test
fun `invalid login`() {
runBlocking {
//prepare
val validator = mock<Validator> {
on { errorFor(any()) }
.thenReturn("Something wrong")
}
val authNavigator = spy(loginNavigator)
val tracker = spy(ActionsTracker(mock()))
val trackers = Trackers(tracker, ViewsTracker(mock()), ClicksTracker(mock()), ImpressionsTracker(mock()))
assertViewModel(
validation = AuthViewModel.LoginValidation(validator, validator),
authNavigator = authNavigator,
trackers = trackers
) {
email.set("test")
password.set("test")
//assert
assertNull(emailError.get())
assertNull(passwordError.get())
//act
login()
//assert
assertNotNull(emailError.get())
assertNotNull(passwordError.get())
verify(authNavigator, never()).navigateToHome()
verify(tracker, never()).userPropertiesChanged(any(), any())
}
}
}