Previously for unit testing ViewModels that depend...
# compose
j
Previously for unit testing ViewModels that depend on a
SavedStateHandle
I'd just set the nav argument key and the value I need before instantiating the ViewModel:
Copy code
@Before
fun setup() {
    savedStateHandle[NAV_ARGUMENT_KEY] = someValue
    viewModel = ViewModel(savedStateHandle)
}
Is there a way I can do that with the new type-safe APIs? In my ViewModel I'm using
toRoute()
to get the serializable that represents the nav destination, but I can't figure out how to do the inverse and somehow "save" the serializable into the
SavedStateHandle
.
i
This exact thing was added in beta01, as per the release notes: https://developer.android.com/jetpack/androidx/releases/navigation#2.8.0-beta01
👍 1
So add the
navigation-testing
artifact and use the
SavedStateHandle(yourSerializableInstance)
Note the known issue where this only works in instrumentation or Robolectric tests at the moment: https://issuetracker.google.com/issues/340966212
j
got it, that’s unfortunate but for now it’ll do, thanks a lot
179 Views