Skip to content

Commit de16e91

Browse files
committed
^ d I've changed my mind about not asserting inside inspectChangingView
1 parent 7f242d3 commit de16e91

3 files changed

Lines changed: 4 additions & 44 deletions

File tree

README.md

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,7 @@ func test_incrementOnce_withTestableView() throws {
9292
<!-- endSnippet -->
9393

9494
That's much simpler, hiding the boilerplate that isn't part of the test-specific intent.
95-
96-
### Improvements for safety and scannability
97-
98-
I avoid assertions inside closures. If something goes wrong with the infrastructure and the closure doesn't run, will the test fail? Sometimes the infrastructure ensures this, sometimes it doesn't.
99-
100-
So I like to set up an optional variable, capture the value inside the closure, then check the result on the outside.
101-
102-
<!-- snippet: scannable -->
103-
<a id='snippet-scannable'></a>
104-
```swift
105-
@MainActor
106-
func test_incrementOnce_scannable() throws {
107-
var sut = ContentView()
108-
var count: String?
109-
110-
inspectChangingView(&sut) { view in
111-
try view.find(viewWithAccessibilityIdentifier: "increment").button().tap()
112-
count = try view.find(viewWithAccessibilityIdentifier: "count").text().string()
113-
}
114-
115-
XCTAssertEqual(count, "1")
116-
}
117-
```
118-
<sup><a href='/SampleApp/CounterTests/ContentViewTests.swift#L42-L55' title='Snippet source file'>snippet source</a> | <a href='#snippet-scannable' title='Start of snippet'>anchor</a></sup>
119-
<!-- endSnippet -->
120-
121-
That lets us add blank lines to separate the Arrange/Act/Assert sections of the test.
122-
123-
Now we have a SwiftUI unit test that is safer, and easier to scan!
95+
Now we have an expressive SwiftUI unit test!
12496

12597
### Acknowledgements
12698

SampleApp/Counter/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct ContentView: TestableView {
1212
}
1313
.accessibilityIdentifier("increment")
1414
.padding()
15+
GreetingView()
1516
}
1617
// begin-snippet: trigger
1718
.onAppear { self.viewInspectorHook?(self) }

SampleApp/CounterTests/ContentViewTests.swift

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,13 @@ final class ContentViewTests: XCTestCase {
3131
@MainActor
3232
func test_incrementOnce_withTestableView() throws {
3333
var sut = ContentView()
34+
3435
inspectChangingView(&sut) { view in
3536
try view.find(viewWithAccessibilityIdentifier: "increment").button().tap()
37+
3638
let count = try view.find(viewWithAccessibilityIdentifier: "count").text().string()
3739
XCTAssertEqual(count, "1")
3840
}
3941
}
4042
// end-snippet
41-
42-
// begin-snippet: scannable
43-
@MainActor
44-
func test_incrementOnce_scannable() throws {
45-
var sut = ContentView()
46-
var count: String?
47-
48-
inspectChangingView(&sut) { view in
49-
try view.find(viewWithAccessibilityIdentifier: "increment").button().tap()
50-
count = try view.find(viewWithAccessibilityIdentifier: "count").text().string()
51-
}
52-
53-
XCTAssertEqual(count, "1")
54-
}
55-
// end-snippet
5643
}

0 commit comments

Comments
 (0)