android-compose-mvvm-foodies icon indicating copy to clipboard operation
android-compose-mvvm-foodies copied to clipboard

ViewModel unit tests

Open rsetkus opened this issue 2 years ago • 5 comments

Hi,

Really like the implementation but I am struggling to find a way how to test view models. To be precise, how you would assert all values set to compose State<T> instance. Any ideas?

rsetkus avatar Mar 06 '22 19:03 rsetkus

Hi @catalinghita8. Any ideas on above?

rsetkus avatar Mar 12 '22 23:03 rsetkus

Hi @rsetkus and sorry for the late response. Asserting values of a State<*> object should be pretty straightforward. You can check out an example here.

Additional resource here.

catalinghita8 avatar Mar 13 '22 09:03 catalinghita8

Hi @catalinghita8. Thanks for the links. That was very useful.

I've read your artice and really liked it. Decided to give it a try and implement it on my own. However, it seems quite different compared to the implementation you provided above. I've applied the same workaround but it didn't work. State<*> doesn't accumulate any values.

Apparently it is known issue to Google and they expect to implement an API which would be capable to write all values written to State<*>

Warning: There are no current APIs to wait for writes to State<T> objects in tests.

Do you have any ideas why given implementation unit tests implemented by example isn't working?

rsetkus avatar Mar 25 '22 21:03 rsetkus

I might have to investigate why with this particular architecture has issues on asserting State<*> values. Have you tried defining some tests where the State abstraction is removed?

catalinghita8 avatar Mar 25 '22 21:03 catalinghita8

The way how I imagine testing MVI view model is to assert written values to State by given Intent. Surely, you could replace State with something else, for instance, LiveData and that might work. However, working with Compose and having only State exposed as output in the view model, is pretty neat. This is why I love your article. :thumbsup:

rsetkus avatar Mar 25 '22 21:03 rsetkus