Testing Apples MVC

The designpattern we all use in one way or the other

Jan Olbrich
Aug 29, 2017 · 4 min read

Apple’s official main design pattern for iOS-apps is the Model View Controller. This pattern contains a model (which contains our data), a view (which displays the data) and a controller (that kind of glues everything together). From their point of view, everything can be classified as part of one of this three components. So let’s see how we can test every single component.

Model

This means we already know how to test models. Models expose an api (methods, properties, …) and they do not require any extra work to set up tests for.

View

  • Static
  • Changing parts

We could test the static parts by creating a screenshot of how it’s supposed to look and comparing a current screenshot with this, but in all honesty, this is something for UX. As developers I think we’re okay if we just concentrate on the changing parts and interactions of a view. Interestingly enough, this results in us having to test the controller, since it is conveying the interactions.

Controller

  • It does a lot, which means a lot of testing and a lot of interacting parts.
  • How do I test methods of the view lifecycle?
  • View-specific methods, such as tableView(tableView: didSelectRowAt:)
  • How do I test IBActions?
  • How do I test IBOutlets?

Lets tackle every one of these points.

It does a lot

How do I test methods of the view lifecycle?

View-specific methods

IBActions and IBOutlets

Loading ViewController in Unit-Tests

testViewController = TestViewController(nibName:"OfferDetailViewController", bundle: nil)_ = testViewController.view

The little property "view" starts the entire View-Life-Cycle, thus also setting up your IBOutlets.

Okay, some people might say Xibs are the old way (I know some programmers that disagree here) how about storyboards? This is a little bit trickier. You have to load the storyboard and than load the view. What makes it even trickier is that you have to enter a Storyboard ID for your ViewController. Otherwise you won’t be able to find it in your storyboard.

So what does the code look like to inflate a storyboard view?

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let testVC = storyboard.instantiateViewController(withIdentifier: "TestViewController")
_ = testVC.view //starting with iOS 8 this is also necessary for storyboards

Conclusion

Next: What about Networking

Previous: Unit-Tests in Swift

Mobile Quality

Everything you need to improve the quality of your mobile app

)

Jan Olbrich

Written by

iOS developer focused on quality and continuous delivery

Mobile Quality

Everything you need to improve the quality of your mobile app

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade