Improve your unit tests with Swift tuples

When writing unit tests for your apps, often you quickly reach a point where setting up the dependencies for your SUT spans over multiple lines.

Take the following test case for an fictitious view model (never heard of MVVM? Take a look at the fantastic introduction on objc.io by Ash Furrow). We just want to assert that after calling the delete method (L11) on the view model the delegate’s correct callback method is invoked.

The test may look like this (given that we wrote mocks for the `APIClientType` and `ProfileViewModelDelegate` protocols).

The setup already takes 60% of the entire test method body. We could surely move the code into `setUp`, but I tend to have the initialisation in the test methods itself, as some do not require all dependencies to be in place.

One alternative which I started using lately is extracting the set up logic into a private helper method on the test case itself and using a tuple as a return type delivering all required dependencies.

We can simply decompose the return value of the helper function binding only those values that are important for the concrete test case at hand. In this one we have omitted the `FakeAPIClient`, as we do not need to do any further configuration on it. But we may want the fake client to return an error upon calling the delete method. We could simply add a boolean property `errorOnNext` and set it after decomposition.

Our test method now focusses on the test case itself and the initialisation has been boiled down to a minimum.