@@ -96,8 +96,7 @@ This is why controlling dependencies is important. It allows us to substitute a
96
96
is deterministic in tests, such as one that simply increments by 1 every time it is invoked.
97
97
98
98
The library comes with a controlled UUID generator and can be accessed by using the
99
- [`@Dependency `][dependency- property- wrapper- docs] property wrapper to add a dependency to the
100
- `Todos` reducer:
99
+ `@Dependency ` property wrapper to add a dependency to the `Todos` reducer:
101
100
102
101
```swift
103
102
struct Todos: ReducerProtocol {
@@ -145,8 +144,7 @@ control dependencies that interact with outside systems.
145
144
## Using library dependencies
146
145
147
146
The library comes with many common dependencies that can be used in a controllable manner, such as
148
- date generators, schedulers, random number generators, UUID generators, and more. A full
149
- list can be seen in the documentation for [`DependencyValues`][dependency- values- docs].
147
+ date generators, schedulers, random number generators, UUID generators, and more.
150
148
151
149
For example, suppose you have a feature that needs access to a date initializer, the main queue
152
150
for time- based asynchrony, and a UUID initializer. All 3 dependencies can be added to your feature's
@@ -191,13 +189,12 @@ func testTodos() async {
191
189
192
190
Although the library comes with many controllable dependencies out of the box, there are still
193
191
times when you want to register your own dependencies with the library so that you can use the
194
- [ `@Dependency `][dependency - property - wrapper - docs] property wrapper. Doing this is quite similar to
195
- registering an [environment value][environment- values- docs] in SwiftUI.
192
+ `@Dependency ` property wrapper. Doing this is quite similar to registering an
193
+ [environment value][environment- values- docs] in SwiftUI.
196
194
197
- First you create a type that conforms to the [`DependencyKey`][dependency- key- docs]
198
- protocol. The minimum implementation you must provide is a `liveValue`, which is the value used
199
- when running the app in a simulator or on device, and so it's appropriate for it to actually make
200
- network requests to an external server:
195
+ First you create a type that conforms to the `DependencyKey` protocol. The minimum implementation
196
+ you must provide is a `liveValue`, which is the value used when running the app in a simulator or
197
+ on device, and so it's appropriate for it to actually make network requests to an external server:
201
198
202
199
```swift
203
200
private enum APIClientKey: DependencyKey {
@@ -211,8 +208,8 @@ will be used while running features in an Xcode preview. You don't need to worry
211
208
values when you are just getting started, and instead can
212
209
[add them later](#Live - preview- and- test- dependencies).
213
210
214
- Finally, an extension must be made to [ `DependencyValues`][dependency - values - docs] to expose a
215
- computed property for the dependency:
211
+ Finally, an extension must be made to `DependencyValues` to expose a computed property for the
212
+ dependency:
216
213
217
214
```swift
218
215
extension DependencyValues {
@@ -224,7 +221,7 @@ extension DependencyValues {
224
221
```
225
222
226
223
With those few steps completed you can instantly access your API client dependency from any
227
- feature's reducer by using the [ `@Dependency `][dependency - property - wrapper - docs] property wrapper:
224
+ feature's reducer by using the `@Dependency ` property wrapper:
228
225
229
226
```swift
230
227
struct Todos: ReducerProtocol {
@@ -274,11 +271,11 @@ That can save a little bit of boilerplate.
274
271
275
272
## Live, preview and test dependencies
276
273
277
- In the previous section we showed that to conform to [ `DependencyKey`][dependency - key - docs] you must
278
- provide _at least_ a `liveValue`, which is the default version of the dependency that is used when
279
- running on a device or simulator. The `DependencyKey` protocol inherits from a base protocol,
280
- [ `TestDependencyKey`][test - dependency - key - docs] , which has 2 other requirements, `testValue`
281
- and `previewValue`. Both are optional and delegate to `liveValue` if not implemented.
274
+ In the previous section we showed that to conform to `DependencyKey` you must provide _at least_
275
+ a `liveValue`, which is the default version of the dependency that is used when running on a
276
+ device or simulator. The `DependencyKey` protocol inherits from a base protocol,
277
+ `TestDependencyKey`, which has 2 other requirements, `testValue` and `previewValue`. Both are
278
+ optional and delegate to `liveValue` if not implemented.
282
279
283
280
If you implement a static `testValue` property on your key, that value will be used when running
284
281
your feature in a ``TestStore``. This is a great opportunity to supply a mocked version of the
@@ -339,12 +336,11 @@ extension APIClient: TestDependencyKey {
339
336
}
340
337
```
341
338
342
- The other requirement of [`TestDependencyKey`][test- dependency- key- docs] is `previewValue`, and if
343
- this value is implemented it will be used whenever your feature is run in an Xcode preview.
344
- Previews are similar to tests in that you usually do not want to interact with the outside world,
345
- such as making network requests. In fact, many of Apple's frameworks do not work in previews, such
346
- as CoreLocation, and so it will be hard to interact with your feature in previews if it touches
347
- those frameworks.
339
+ The other requirement of `TestDependencyKey` is `previewValue`, and if this value is implemented
340
+ it will be used whenever your feature is run in an Xcode preview. Previews are similar to tests in
341
+ that you usually do not want to interact with the outside world, such as making network requests.
342
+ In fact, many of Apple's frameworks do not work in previews, such as Core Location, and so it will
343
+ be hard to interact with your feature in previews if it touches those frameworks.
348
344
349
345
However, previews are dissimilar to tests in that it's fine for dependencies to return some mock
350
346
data. There's no need to deal with " unimplemented" clients for proving which dependencies are
@@ -499,10 +495,6 @@ struct Onboarding: ReducerProtocol {
499
495
This will cause the `Feature` reducer to use a mock user defaults and database dependency, as well
500
496
as any reducer `Feature` uses under the hood, _and_ any effects produced by `Feature`.
501
497
502
- [dependency- values- docs]: https: // pointfreeco.github.io/swift-composable-architecture/main/documentation/dependencies/dependencyvalues
503
498
[swift- identified- collections]: https: // github.com/pointfreeco/swift-identified-collections
504
- [dependency- property- wrapper- docs]: https: // pointfreeco.github.io/swift-composable-architecture/main/documentation/dependencies/dependency
505
499
[environment- values- docs]: https: // developer.apple.com/documentation/swiftui/environmentvalues
506
- [dependency- key- docs]: https: // pointfreeco.github.io/swift-composable-architecture/main/documentation/dependencies/dependencykey
507
- [test- dependency- key- docs]: https: // pointfreeco.github.io/swift-composable-architecture/main/documentation/dependencies/testdependencykey
508
500
[xctest- dynamic - overlay- gh]: http: // github.com/pointfreeco/xctest-dynamic-overlay
0 commit comments