Test a ViewModel Class
Testing a ViewModel takes what we did in the previous section and adds a bit more complexity. Instead of just the response from a function, we now have to deal with LiveData and Observer objects. Luckily, the libraries we added at the beginning of the chapter will make our life easier here.
Add a new class inside the test folder called GameViewModelTests. This class at the end will contain two tests: one checking GameViewModel.startGame and the other dealing with GameViewModel.roll. Before we get into the tests, though, let’s get all the setup work out of the way.
Set Up the Test Class
Most of this class will look the same at a high level as GameHandlerTests in that we have a setup function with the @Before annotation ...
Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.