Mikolaj
08/25/2023, 10:07 AMdata class State(
val button: Button
) {
data class Button(
val text: String,
val onClick: () -> Unit
)
}
I am assigning methods of my viewModel to lambdas inside state.
I tried to run a unit test but it fails due to lambda inequality. My test looks like this:
@Test
fun `should assign correct lambdas`() = runTest {
val vm = MyViewModel(backgroundScope)
vm.test(this) {
expectInitialState()
expectState(
MyViewModel.State(
Button(
text = "OK",
onClick = vm::someAction // test fails here
)
)
)
}
}
Is there some way to check the equality of lambdas in that scenario?Mikolaj Leszczynski
08/25/2023, 10:16 AMMikolaj
08/25/2023, 10:26 AMclass MyViewModel(
coroutineScope: CoroutineScope,
) : ContainerHost<State, SideEffect> {
override val container: Container<State, SideEffect> = coroutineScope.container(
State(
State.Button(
text = "OK",
onClick = this::someAction
)
)
)
val stateFlow: StateFlow<State> = container.stateFlow
val sideEffectFlow: Flow<SideEffect> = container.sideEffectFlow
fun someAction() {
// perform some action
}
}
Mikolaj Leszczynski
08/25/2023, 10:28 AMMikolaj Leszczynski
08/25/2023, 10:29 AMMikolaj Leszczynski
08/25/2023, 10:30 AMonClick = { someAction() }
as this creates a new lambda instanceMikolaj
08/25/2023, 11:18 AMreduce
like this:
fun someAction() = intent {
reduce { state.copy(button = state.button!!.copy("NOT OK", ::someAction)) }
}
the test fails with no apparent reason. But when I change the return type of my action to Job
(the return type of intent
) I start getting compiler error: Type checking has run into a recursive problem.
😄
Seems like compiler does not care about return type as long as it is Unit
🤷Mikolaj
08/25/2023, 11:18 AMMikolaj Leszczynski
08/25/2023, 11:19 AMMikolaj Leszczynski
08/25/2023, 11:20 AMfun someAction() { intent { ...
insteadMikolaj Leszczynski
08/25/2023, 11:21 AMJob
anywhereMikolaj
08/25/2023, 11:22 AM