Luke97
Luke97

Reputation: 539

How to write unit tests for the AppDelegate?

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

Upvotes: 9

Views: 6593

Answers (2)

Jon Reid
Jon Reid

Reputation: 20980

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.

Upvotes: 7

Lou Franco
Lou Franco

Reputation: 89172

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.

Upvotes: 4

Related Questions