Using the new Compose typesafe navigation, what’s ...
# compose-android
y
Using the new Compose typesafe navigation, what’s the best way to run a test that depends on the
Route
object in
SavedStateHandle
? For example, I have the following code:
Copy code
private val route = savedStateHandle.toRoute<MyRoute>()
How can I instantiate the
SavedStateHandle
object in my tests? Specifically, what should I provide as a key for the map? I’ve tried code similar to the following, but none of the
Route
properties are being passed :
Copy code
val savedStateHandle = SavedStateHandle(mapOf(MyRoute::class.qualifiedName to fakeRoute))
i
m
is there a way to run this inside regular, junit, unit tests?
I’m getting
android.os.Bundle
methods not mocked error. I have
testOptions.unitTests.isReturnDefaultValues = true
in gradle
i
As seen in the exact documentation of that method, you need to use Robolectric
😞 1
j
Not sure if it is the best idea (as it depends on internals) but we currently mocked the SavedStateHandle using mockk to use it in plain unit tests. Here an example:
i
Now that https://issuetracker.google.com/issues/398265336 is solved, the next alpha of Navigation will let you use
jvmTest
(no Robolectric) with
SavedStateHandle
j
Thx for the heads up. We will try that with the next version.
m
thanks, that is awesome. I used to use similar mockk setup
p
I am having the same issue in a compose multiplatform project and I cannot seem to be able to solve. @Ian Lake I am trying to write unit tests (not instrumentation) for viewmodels that make use of safe navigation arguments via the
SavedStateHandle.toRoute
api. My tests are in
commonTest
target. My app is targeting android and ios (iosX64, iosArm64 and iosSimulatorArm64).
androidx.navigation:navigation-testing
does not seem to be compatible with ios. I have tried to use the
androidUnitTest
with Robolectric but I get
java.lang.NullPointerException: encode(...) must not be null
I have not tried to create a new jvm target and write tests there because I am not needing it and creating it would mean a number of actual implementations that would not serve any purpose. I could not find any source online with a concrete example, any help or guidance is more than welcome.