Skip to content

Commit 121a428

Browse files
authored
Rename test scoping trait (#344)
We can make it consistent with the old trait name in case people are extending it.
1 parent 262a5b4 commit 121a428

File tree

2 files changed

+2
-59
lines changed

2 files changed

+2
-59
lines changed

Sources/Dependencies/Documentation.docc/Articles/Testing.md

-57
Original file line numberDiff line numberDiff line change
@@ -153,63 +153,6 @@ class FeatureTests: XCTestCase {
153153
}
154154
```
155155

156-
157-
<!--
158-
## Altered execution contexts
159-
160-
It is possible to completely alter the execution context in which a feature's logic runs, which is
161-
great for tests. It means your feature doesn't need to actually make network requests just to test
162-
how your feature deals with data returned from an API, and your feature doesn't need to interact
163-
with the file system just to test how data gets loaded or persisted.
164-
165-
The tool for doing this is ``withDependencies(_:operation:)``, which allows you to specify
166-
which dependencies should be overridden for the test, and then construct your feature's model in
167-
that context:
168-
169-
```swift
170-
@Test
171-
func feature() async {
172-
let model = withDependencies {
173-
$0.continuousClock = .immediate
174-
$0.date.now = Date(timeIntervalSince1970: 1234567890)
175-
} operation: {
176-
FeatureModel()
177-
}
178-
179-
// Call methods on `model` and make assertions
180-
}
181-
```
182-
183-
As long as all of your dependencies are declared with `@Dependency` as instance properties on
184-
`FeatureModel`, its entire execution will happen in a context in which any reference to
185-
`continuousClock` is an `ImmediateClock` and any reference to `date.now` will always report that
186-
the date is "Feb 13, 2009 at 3:31 PM".
187-
188-
> Note: If you are using XCTest it is important to note that if `FeatureModel` creates _other_
189-
> models inside its methods, then it has to be careful about how it does so. In order for
190-
> `FeatureModel`'s dependencies to propagate to the new child model, it must construct the child
191-
> model in an altered execution context that passes along the dependencies. The tool for this is
192-
> ``withDependencies(from:operation:fileID:filePath:line:column:)`` and can be used simply like
193-
> this:
194-
>
195-
> ```swift
196-
> @Observable
197-
> class FeatureModel {
198-
> // ...
199-
>
200-
> func buttonTapped() {
201-
> self.child = withDependencies(from: self) {
202-
> ChildModel()
203-
> }
204-
> }
205-
> }
206-
> ```
207-
>
208-
> This guarantees that when `FeatureModel`'s dependencies are overridden in tests that it will also
209-
> trickle down to `ChildModel`.
210-
211-
-->
212-
213156
## Changing dependencies during tests
214157

215158
While it is most common to set up all dependencies at the beginning of a test and then make

Sources/DependenciesTestSupport/TestTrait.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#if swift(>=6.1)
77
@_documentation(visibility: private)
8-
public struct _DependenciesScopeTrait: TestScoping, TestTrait, SuiteTrait {
8+
public struct _DependenciesTrait: TestScoping, TestTrait, SuiteTrait {
99
let updateValues: @Sendable (inout DependencyValues) throws -> Void
1010

1111
@TaskLocal static var isRoot = true
@@ -29,7 +29,7 @@
2929
}
3030
}
3131

32-
extension Trait where Self == _DependenciesScopeTrait {
32+
extension Trait where Self == _DependenciesTrait {
3333
/// A trait that quarantines a test's dependencies from other tests.
3434
///
3535
/// When applied to a `@Suite` (or `@Test`), the dependencies used for that suite (or test)

0 commit comments

Comments
 (0)