You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-6Lines changed: 36 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,8 +95,8 @@ One way to do this is to create an instance of the `AnalyticsClient` type that s
95
95
importXCTest
96
96
97
97
extensionAnalyticsClient {
98
-
staticletfailing=Self(
99
-
track: { _inXCTFail("AnalyticsClient.track is unimplemented.") }
98
+
staticletunimplemented=Self(
99
+
track: { _inXCTFail("\(Self.self).track is unimplemented.") }
100
100
)
101
101
}
102
102
```
@@ -106,7 +106,7 @@ With this you can write a test that proves analytics are never tracked, and even
106
106
```swift
107
107
functestValidation() {
108
108
let viewModel =LoginViewModel(
109
-
analytics: .failing
109
+
analytics: .unimplemented
110
110
)
111
111
112
112
...
@@ -115,7 +115,7 @@ func testValidation() {
115
115
116
116
However, you cannot ship this code with the target that defines `AnalyticsClient`. You either need to extract it out to a test support module (which means `AnalyticsClient` must also be extracted), or the code must be confined to a test target and thus not shareable.
117
117
118
-
However, with `XCTestDynamicOverlay`we can have our cake and eat it too 😋. We can define both the client type and the failing instance right next to each in application code without needing to extract out needless modules or targets:
118
+
However, with XCTest Dynamic Overlay we can have our cake and eat it too 😋. We can define both the client type and the unimplemented instance right next to each in application code without needing to extract out needless modules or targets:
119
119
120
120
```swift
121
121
structAnalyticsClient {
@@ -130,12 +130,42 @@ struct AnalyticsClient {
130
130
importXCTestDynamicOverlay
131
131
132
132
extensionAnalyticsClient {
133
-
staticletfailing=Self(
134
-
track: { _inXCTFail("AnalyticsClient.track is unimplemented.") }
133
+
staticletunimplemented=Self(
134
+
track: { _inXCTFail("\(Self.self).track is unimplemented.") }
135
135
)
136
136
}
137
137
```
138
138
139
+
XCTest Dynamic Overlay also comes with a helper that simplifies this exact pattern: `XCTUnimplemented`. It creates failing closures for you:
140
+
141
+
```swift
142
+
extensionAnalyticsClient {
143
+
staticlet unimplemented =Self(
144
+
track: XCTUnimplemented("\(Self.self).track")
145
+
)
146
+
}
147
+
```
148
+
149
+
And it can simplify the work of more complex dependency endpoints, which can throw or need to return a value:
0 commit comments