Skip to content

Commit 7d2eb4a

Browse files
authored
Fix dependency test trait for stateful dependencies. (#314)
* Fix dependency test trait for stateful dependencies. * fix older platforms
1 parent 20ad45b commit 7d2eb4a

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

Sources/DependenciesTestSupport/TestTrait.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
/// - value: A dependency value to override for the test.
4444
public static func dependency<Value>(
4545
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
46-
_ value: sending Value
46+
_ value: @autoclosure @escaping @Sendable () -> Value
4747
) -> Self {
48-
Self { [uncheckedValue = UncheckedSendable(value)] in
49-
$0[keyPath: keyPath] = uncheckedValue.wrappedValue
48+
Self {
49+
$0[keyPath: keyPath] = value()
5050
}
5151
}
5252

@@ -79,9 +79,9 @@
7979
/// - keyPath: A key path to a dependency value.
8080
/// - value: A dependency value to override for the test.
8181
public static func dependency<Value: TestDependencyKey>(
82-
_ value: Value
82+
_ value: @autoclosure @escaping @Sendable () -> Value
8383
) -> Self where Value == Value.Value {
84-
Self { $0[Value.self] = value }
84+
Self { $0[Value.self] = value() }
8585
}
8686

8787
/// A trait that overrides a test's or suite's dependencies.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#if canImport(Testing)
2+
import Dependencies
3+
import DependenciesTestSupport
4+
import Foundation
5+
import Testing
6+
7+
@Suite(.dependency(\.uuid, .incrementing))
8+
struct TestTraitTests {
9+
@Dependency(\.uuid) var uuid
10+
11+
@Test func statefulDependency1() async throws {
12+
for index in 0...100 {
13+
#expect(uuid() == UUID(index))
14+
try await Task.sleep(for: .milliseconds(1))
15+
}
16+
}
17+
18+
@Test func statefulDependency2() async throws {
19+
for index in 0...100 {
20+
#expect(uuid() == UUID(index))
21+
try await Task.sleep(for: .milliseconds(1))
22+
}
23+
}
24+
25+
@Test func statefulDependency3() async throws {
26+
for index in 0...100 {
27+
#expect(uuid() == UUID(index))
28+
try await Task.sleep(for: .milliseconds(1))
29+
}
30+
}
31+
}
32+
#endif

0 commit comments

Comments
 (0)