I have a keen interest in finding different solutions to a problem. Currently exploring different architectures for iOS applications.
A Quick Overview of MVVM
In my previous article "MVVM an Improvement on MVC," we have seen how MVVM separates the View State from the Controller in ViewModel. We can drive View states represented by ViewModel in isolation. This provides us the ability to write test cases to verify different behaviors of View.
The Test Subject
In this tutorial we will be testing "Video Games" application. It's a simple app that loads and display a list of Games on home screen. Our app flow is as follows:
- App will start loading Games on View load.
- During loading, app will display a spinner on the screen.
- If data gets loaded successfully, we will display items in Table View.
- If loading ends with error, then app will display "Unable to load games. Please try again !" message.
- If loaded items count is 0 then app will display "No games available at the moment." message.
I have compiled a starter project so that you can practice as we proceed. Download the project from here and open the project in Starter folder to get started.
Games List View Model
Before we start writing test cases of our GamesListViewModel, lets take a look at its behaviour. You can find GamesListViewModel in the root of starter porject.
In GameListViewModel we are exposing all the state information, state change notifications (GameListViewModelDelegate) and actions that UI can initiate. GameListViewModel has one dependency that is DataService which allows us to load Game list.
Prepare Test Enviornment
One of the Unit Testing principle is to isolate the test subject and simulate its dependencies in order to test all the possible cases.
Recommended for you
Our test subject GameListViewModel has one dependency i.e. DataService. In order to isolate and simulate enviornment of GameListViewModel, we have to mock DataService behaviours. Following is the mock servicer we will be using:
The GameListViewModel notifies events through GameListViewModelDelegate and to monitor these events we will use the following:
Lets Start Testing
In our first test case we will test the behaviour of GamesListViewModel if data is loaded successfully with none empty list. To do that we tell MockDataService to send games list with one item and will check the GamesListViewModel response using MonitorGamesListViewModelDelegate.
In this test case we are performing five steps:
- Defined expectations that should be fulfilled before test exit.
- Setting up the dependencies to send a valid response with none empty games list.
- Setting up the response monitor that listens to events and asserts the expected state of ViewModel.
- Simulating "loadGames" action on ViewModel.
- Waiting for expectations to be fulfilled in a particular sequence.
Now we will test what if loading data request fails with an error. According to our requirement showError should be true and errorMessage should say "Unable to load games. Please try again !"
Now our final case, data loaded successfully but game list is empty. Desired result in this case is, showError should be true and errorMessage should say "No games available at the moment."
We have seen how MVVM makes it easy to simulate the user behaviour without dealing with the View. We have written three test cases to see how we can test different states of the View.
You can download the final project from here. Also if you have any question or suggestion drop a comment below.