Open ssiddh opened 2 years ago
Hi, sorry for the late answer, but if anyone is interested, verify(1)
is wrong, because you're setting propertyA
twice. First when you set the initial state, then when you call doSomething
. The rest works fine for me:
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.test.MavericksTestRule
import com.airbnb.mvrx.withState
import io.mockk.confirmVerified
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
data class SomeState(val propertyA: String, val propertyB: String, val propertyC: String) : MavericksState
interface Tracker {
fun fireAnalyticsEvent()
}
class SomeViewModel(initialState: SomeState, tracker: Tracker) : MavericksViewModel<SomeState>(initialState) {
init {
onEach(SomeState::propertyA) {
println("*** propertyA: $it")
tracker.fireAnalyticsEvent()
}
}
fun doSomething() = setState { copy(propertyA = "a1") }
}
@OptIn(ExperimentalCoroutinesApi::class)
class SomeViewModelTest {
@get:Rule
val mavericksTestRule = MavericksTestRule(testDispatcher = UnconfinedTestDispatcher())
@Test
fun sampleTest() {
// given
val tracker = mockk<Tracker> { justRun { fireAnalyticsEvent() } }
val sut = SomeViewModel(SomeState("a", "b", "c"), tracker)
// when
sut.doSomething()
// then
withState(sut) { state -> assertTrue(state.propertyA == "a1") }
verify(exactly = 2) { tracker.fireAnalyticsEvent() }
confirmVerified(tracker)
}
}
This prints:
*** propertyA: a
*** propertyA: a1
I am trying to unit test certain flows on the viewmodel which are triggered
onEach
subscription of the a specific property. https://airbnb.io/mavericks/#/core-concepts?id=subscribing-to-state-changesI can correctly verify the state transitions in the unit test, but I am not able to verify functions called in response to
onEach
subscription. I looked through the documentation and could not find something that could help. Is there a way to achieve this?example:
PS: Thank you for this amazing library, it is a delight to use.