Skip to content

Commit 36588b9

Browse files
Add tests for viewDidLoad
1 parent 901e331 commit 36588b9

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Tests/UICoreTests/ViewControllerTests.swift

+52
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,56 @@ final class ViewControllerTests: XCTestCase {
5959

6060
XCTAssert(sut.next === expectedResult)
6161
}
62+
63+
func testViewDidLoadMethodCalledAfterLazyViewLoad() {
64+
let sut = MockViewController()
65+
66+
let expectation = self.expectation(description: "viewDidLoad should be called")
67+
68+
sut.viewDidLoadBlock = {
69+
expectation.fulfill()
70+
}
71+
72+
_ = sut.view
73+
74+
wait(for: [expectation], timeout: 0.1)
75+
}
76+
77+
func testViewDidLoadMethodCalledAfterManualViewLoad() {
78+
let sut = MockViewController()
79+
80+
let expectation = self.expectation(description: "viewDidLoad should be called")
81+
82+
sut.viewDidLoadBlock = {
83+
expectation.fulfill()
84+
}
85+
86+
sut.loadViewIfNeeded()
87+
88+
wait(for: [expectation], timeout: 0.1)
89+
}
90+
91+
func testViewDidLoadMethodNotCalledIfViewNotLoaded() {
92+
let sut = MockViewController()
93+
94+
let expectation = self.expectation(description: "viewDidLoad should not be called")
95+
expectation.isInverted = true
96+
97+
sut.viewDidLoadBlock = {
98+
expectation.fulfill()
99+
}
100+
101+
wait(for: [expectation], timeout: 0.1)
102+
}
103+
}
104+
105+
final class MockViewController: ViewController {
106+
var viewDidLoadBlock: () -> Void = {
107+
XCTFail("Not implemented")
108+
}
109+
110+
override func viewDidLoad() {
111+
super.viewDidLoad()
112+
viewDidLoadBlock()
113+
}
62114
}

0 commit comments

Comments
 (0)