Dependency Injection using Storyboards

You’re not restricted anymore!

Photo by Ben Wicks on Unsplash

Prerequisites:

Dependency Injection

Motivation

High level modules should not be dependent on low-level modules, but rather abstractions. If this is true, we can swap out classes rather than making a fixed dependency for a concrete class.

Why it matters

If you are using a network service (say to make those GET requests) to test screens that depend on data from the network service you will need to wait for the request to be made. Now not only is that rather boring to wait for (in larger projects this will take a great deal of time), but is seen as unprofessional as if your network goes down your tests will fail — but the problem actually isn’t your code so the test isn’t performing it’s job!

The implementation!

iOS 13 and above

Whacking a Master-Detail couplet into a storyboard is something that most iOS developers are familiar with:

iOS 12 and below

Segue You’ll need to change DetailViewController

Doing better

That all seems fine. But what about the initial view controller? The solutions above are rather unsatisfactory.

Conclusion

This has been a rather long article, and hopefully has been some help to you. Not many articles cover Testing so hopefully this offers some value for you!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store