Migrating to swift: Thoughts about unit testing

If you are planning to start a new iOS project you will certainly choose swift as programming language and this decision would be without doubt the right one.

But a lot of companies have already big iOS apps with thousands LoC of legacy objective-c. ImmobileinScout24 is one of those companies with a code base started 5 years ago. Several month ago we decided to implement new features and code refactoring in swift.
Our goal was to have a smooth migration to the amazing new open source language swift. That means we will have a mixed code base for a pretty long time.

One challenge we had to face doing this migration is to keep our more than 2000 unit test cases intact and to keep improving our test coverage.
For new classes written in swift we could “easily” write the unit tests using swift. But what about swift classes used in objective-c code? Usually you need to mock those swift classes in your objective-c unit test and as you may know it is not so easy to do so yet.

Let me show you an example of a simple UIViewController in my case an UserViewController that uses a tracking service to track that the screen has been shown.
In the first step both classes the UserViewController and the TrachingService are written in objective-c:

Because our swift service inherits from NSObject our objective-c test should (theoretically) works without changes. We could (theoretically) create an OCMockObject from the swift class. But the problem is that we still have a limitation that apple did not fix yet. We cannot access swift classes from an objective-c unit test.

To solve that i wrote a protocol in the test class declaring the same method defined in the swift class:

@protocol TrackingServiceForTest
- (void) trackUserScreenShown;
@end

Then i used that protocol to create my mock and it worked fine. The unit test would look like:

Migrating a big objective-c iOS-App maybe a challenging task and may take a lot of time to complete. For me there are a lot of reasons to believe that it’s worth the effort. Keeping your unit tests performing well during the migration is highly recommended to ensure your app quality.