I've a composable that takes as parameters several...
# compose-android
a
I've a composable that takes as parameters several viewModels. I'm trying to androidTest said composable. How can I instantiate real viewModels in my instrumentation tests?
Or is mocking the only solution?
p
What is wrong with depending on an abstract ViewModel class and provide real vs tests implementations depending on the environment. Although this is not popular TBH
Although you can also live the ViewModel as is and rather use fake in the ViewModel constructor dependecies. Aren't you able to do something like
Copy code
class MyViewModel(
  dep1: Dependency1Intf,
  dep2: Dependency2Intf
)
Copy code
fake1: Dependency1Intf = FakeDep1()
fake2: Dependency2Intf = FakeDep2()
Copy code
MyViewModel(fake1, fake2)
a
I ended up mockking. Easiest way given that composables can't be used in instrumentation testing.
p
Mocking is good, basically you can achieve good testing both ways mocking and faking.
a
Mocking is brittle though as your implementation gets complication you need to update the mocked values stubbed method so that they don't start returning null or giving NPEs