How to test for viewDidLoad in iOS
Issue #52
Suppose we have the following view controller
1 | class ListController: UIViewController { |
Get to know viewDidLoad
We know that viewDidLoad is called when view is created the first time. So in the the Unit Test, if you use viewDidLoad to trigger, you will fall into a trap
1 | func testSetup() { |
Why is viewDidLoad called twice?
- It is called once in your test
- And in your
viewDidLoadmethod, you accessview, which is created the first time, hence it will triggerviewDidLoadagain
The correct way
The best practice is not to trigger events yourself, but do something to make event happen. In Unit Test, we just access view to trigger viewDidLoad
1 | func testSetup() { |