I am doing a lot of setup inside my app delegate (mainly for CoreData) inside of applicationDidFinishLaunchingWithOptions
. And was curious how i would go about testing code inside the appDelegate? Thanks
Asked
Active
Viewed 5,829 times
9

Rajamohan S
- 7,229
- 5
- 36
- 54

Luke97
- 529
- 3
- 11
- 27
2 Answers
7
Step one: Stop using your regular application delegate during testing. This avoids the "it will be called at launch" problem, and will likely also speed up your tests. See https://qualitycoding.org/ios-app-delegate-testing/
Step two: Now that your regular application delegate isn't invoked when tests are launched, directly call its methods from tests.

Jon Reid
- 20,545
- 2
- 64
- 95
-
How do we access the regular app delegate now that `UIApplication.shared.delegate` contains our test version? – Declan McKenna Oct 30 '19 at 11:38
-
1I'd create the test version as a copy of the regular app delegate, so you start with all the pieces you need. Then trim them down so that your tests can control the return values. Chapter 4 of my book https://pragprog.com/book/jrlegios/ios-unit-testing-by-example focuses on the app delegate. – Jon Reid Oct 30 '19 at 16:29
4
Move the functionality into smaller functions or other classes that you can test.
If you keep things in the App Delegate class, you can access them the normal way since the unit tests are linked to the app and the app is actually run. But you cannot call applicationDidFinishLaunchingWithOptions
and expect it to work. It will be called by iOS at the start, like normal.

Lou Franco
- 87,846
- 14
- 132
- 192