Mastering MVVM With Swift
Ready, Set, Test
|1||Welcome to Mastering MVVM With Swift 03:25|
|2||Is the Model-View-Controller Pattern Dead 07:20|
|3||How Does the Model-View-ViewModel Pattern Work 04:36|
|4||Meet Cloudy 07:46|
|5||What Is Wrong With Cloudy 03:43|
|6||A Quick Recap Plus 02:32|
|7||Time to Create a View Model 08:03|
|8||Put the View Model to Work Plus 05:05|
|9||Rinse and Repeat Plus 08:08|
|10||Using MVVM In the Settings View Plus 06:56|
|11||Adding Protocols to the Mix Plus 04:05|
|12||Making Table View Cells Autoconfigurable Plus 04:06|
|13||Supercharging MVVM With Protocols Plus 06:26|
|14||Ready, Set, Test 02:47|
|15||Testing Your First View Model 07:59|
|16||Using Stubs for Better Unit Tests 08:17|
|17||A Few More Unit Tests Plus 04:07|
|18||Taking MVVM to the Next Level 05:28|
|19||What Are the Options Plus 03:08|
Because we moved a fair bit of logic from the view controllers of the project to the view models, we gained an important advantage, improved testability. As I mentioned earlier in this series, unit testing view controllers is known to be difficult. View models, however, are easy to test and that is the focus of the next few episodes.
Adding a Unit Test Target
Before we can start testing the view models we created earlier in this series, we need to add a target for the unit tests. Select the project in the Project Navigator, click the plus button at the bottom, and choose iOS Unit Testing Bundle.
The defaults are just fine. Make sure Language is set to Swift and Target to be Tested is set to Cloudy.
The Cloudy project should now have two targets, Cloudy and CloudyTests.
Organizing the Unit Test Target
I usually create several groups in the unit testing bundle to keep files and folders organized. I create a group named Supporting Files for the Info.plist file, a group for stubs, a group for extensions, and a group for the test cases. This is what you should end up with. I also removed the test case Xcode created for us, CloudyTests.swift. We start from scratch.
We need to update the path to the Info.plist file in the build settings of the CloudyTests target. Choose the CloudyTests target from the list of targets, select Build Settings at the top, and search for the Info.plist File build setting in the Packaging section. Change the path from CloudyTests/Info.plist to CloudyTests/Supporting Files/Info.plist.
Let's run the test suite to make sure the unit test target is correctly configured. We don't have any unit tests yet, but that isn't a problem. We only verify that the unit test target is ready and properly configured.
Choose a simulator from the list of devices and run the test suite by choosing Test from Xcode's Product menu. The application is installed in the simulator and the test suite is run. No errors or warnings should be visible.
In the next episode, we write unit tests for the view models of the settings view controller.