Test-Driven Development — How to test viewDidLoad?

func test_ViewDidLoad_...() {
let sut = ViewController()
_ = sut.view
XCAssert(...)
}
Apple Documentation about viewDidLoad():
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the
loadView()
method. You usually override this method to perform additional initialization on views that were loaded from nib files.
Apple Documentation about loadView():
You should never call this method directly. The view controller calls this method when its
view
property is requested but is currentlynil
. This method loads or creates a view and assigns it to theview
property.
This is the reason why we called “view” and you should never call viewDidLoad() directly. Accessing this property cause the view to be loaded automatically. The root view of the view controller gets assigned and the view hierarchy gets loaded into memory, then viewDidLoad gets called.
loadViewIfNeeded()
AppleDocumentation about loadViewIfNeeded():
Loads the view controller’s view if it has not yet been loaded.
You can also use loadViewIfNeeded since it will also trigger viewDidLoad.
func test_ViewDidLoad_...() {
let sut = ViewController()
sut.loadViewIfNeeded()
XCAssert(...)
}