Unit test refactors #9

merged 10 commits into from Dec 1, 2015


None yet

2 participants


Here's a first pass at making ViewController a little more testable. I'm following the techniques I've outlined in Better Unit Testing with Swift, namely:

  • inject static dependencies
  • make dependencies protocols
  • make all value types equatable

First, I made USPSManager conform to a new protocol, PackageManager. This allows us to create a mock for use under tests. Then, to make assertions easier, I moved the class methods to instance methods. Now the mock can record each parameter that was passed to it under test (and call the completion block with mock data!).

This required opening up the packageManager variable on ViewController. Ideally, this would be a let variable and injected via the initializer. However, I wasn't able to get this to work with storyboards so I figured a lazy-evaluated var was a good tradeoff.

Finally, the tests set up some mock package tracking information then assert that the table shows said data.


👍 Looks great, thanks!

@SixFiveSoftware SixFiveSoftware merged commit 299041d into ConceptsInCode:develop Dec 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment