Hello hello folks. I;m following this toturial to ...
# compose-desktop
l
Hello hello folks. I;m following this toturial to create Ui tests for my app... but I always receive this error:
Copy code
No ViewModelStoreOwner was provided via LocalViewModelStoreOwner
When viewModels are instantiated by the app inside the test. The app works just fine, but the UI tests never run. I'm using
runComposeUiTest
. Is this a known issue?
z
Known issue as in there always needs to be an owner present through the
LocalViewModelStoreOwner
composition local that any ViewModels can be scoped to. If you're using the nav library, its destinations each provide owners automatically. There's also "global" ones on each platform for the entry points of Compose content, here's the desktop one for example (on Android it's the Activity by default, etc). If you're sure you want ViewModels to be instantiated like this in a test (as opposed to testing smaller parts, or without VMs), you can create your own VMStoreOwner implementation for tests and provide it with a
CompositionLocalProvider
within
setContent
, it shouldn't be too much code.
l
Uhm, so if I get this right I have to instantiate the ViewModels inside the navigation, then it will be provided right? I'm creating some of them outside of the navigation... I'll check if I can create all of them inside the navigation
Thanks for the help Márton 🙂
z
I think that should work, yes, even in tests, though I haven't tried it. Maybe try a small nav graph as an example first, there might be other things missing from the test environment that make this nontrivial.
l
Okay. I'll try in a while and let you know.
Uhm. I did remove all ViewModel initialization outside the navigation. But now I have this error:
Copy code
java.lang.IllegalStateException: CompositionLocal LocalLifecycleOwner not present
	at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.nonAndroid.kt:28)
	at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.nonAndroid.kt:27)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:83)
	at androidx.compose.runtime.LazyValueHolder.getCurrent(ValueHolders.kt:46)
	at androidx.compose.runtime.LazyValueHolder.readValue(ValueHolders.kt:48)
	at androidx.compose.runtime.CompositionLocalMapKt.read(CompositionLocalMap.kt:88)
	at androidx.compose.runtime.ComposerImpl.consume(Composer.kt:2390)
	at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:831)
	at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:231)