Skip to content

Commit 352c68c

Browse files
stephencelisp4checo
authored andcommitted
Avoid failure when setting test dependency values (#1570)
* Avoid failure when setting test dependency values It's currently possible to emit XCTest failures when testing reducers that access dependency values that emit failure upon access, which includes the calendar, locale, time zone, and URL session dependencies. We may need a more holistic solution for folks that define these kinds of dependencies outside the library, but this will make the library-level failures a bit more lenient. * wip * a few more tests Co-authored-by: Brandon Williams <[email protected]> (cherry picked from commit f8554f7088e344b8e4d598648c8fb758a378dcb3) # Conflicts: # Tests/ComposableArchitectureTests/StoreTests.swift # Tests/ComposableArchitectureTests/TestStoreTests.swift
1 parent c20b402 commit 352c68c

File tree

8 files changed

+642
-547
lines changed

8 files changed

+642
-547
lines changed

Examples/TicTacToe/tic-tac-toe/Sources/AuthenticationClient/AuthenticationClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct AuthenticationClient: Sendable {
7272
}
7373

7474
extension AuthenticationClient: TestDependencyKey {
75-
public static var testValue = Self(
75+
public static let testValue = Self(
7676
login: unimplemented("\(Self.self).login"),
7777
twoFactor: unimplemented("\(Self.self).twoFactor")
7878
)

Sources/Dependencies/Dependencies/Calendar.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ extension DependencyValues {
3333
private enum CalendarKey: DependencyKey {
3434
static let liveValue = Calendar.autoupdatingCurrent
3535
static var testValue: Calendar {
36-
XCTFail(#"Unimplemented: @Dependency(\.calendar)"#)
36+
if !DependencyValues.isSetting {
37+
XCTFail(#"Unimplemented: @Dependency(\.calendar)"#)
38+
}
3739
return .autoupdatingCurrent
3840
}
3941
}

Sources/Dependencies/Dependencies/Locale.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ extension DependencyValues {
4545
private enum LocaleKey: DependencyKey {
4646
static let liveValue = Locale.autoupdatingCurrent
4747
static var testValue: Locale {
48-
XCTFail(#"Unimplemented: @Dependency(\.locale)"#)
48+
if !DependencyValues.isSetting {
49+
XCTFail(#"Unimplemented: @Dependency(\.locale)"#)
50+
}
4951
return .autoupdatingCurrent
5052
}
5153
}

Sources/Dependencies/Dependencies/TimeZone.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ extension DependencyValues {
2424
private enum TimeZoneKey: DependencyKey {
2525
static let liveValue = TimeZone.autoupdatingCurrent
2626
static var testValue: TimeZone {
27-
XCTFail(#"Unimplemented: @Dependency(\.timeZone)"#)
27+
if !DependencyValues.isSetting {
28+
XCTFail(#"Unimplemented: @Dependency(\.timeZone)"#)
29+
}
2830
return .autoupdatingCurrent
2931
}
3032
}

Sources/Dependencies/Dependencies/URLSession.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ extension DependencyValues {
7979
private enum URLSessionKey: DependencyKey {
8080
static let liveValue = URLSession.shared
8181
static var testValue: URLSession {
82-
XCTFail(#"Unimplemented: @Dependency(\.urlSession)"#)
82+
if !DependencyValues.isSetting {
83+
XCTFail(#"Unimplemented: @Dependency(\.urlSession)"#)
84+
}
8385
let configuration = URLSessionConfiguration.ephemeral
8486
configuration.protocolClasses = [UnimplementedURLProtocol.self]
8587
return URLSession(configuration: configuration)

Sources/Dependencies/DependencyValues.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import XCTestDynamicOverlay
5555
/// ```
5656
public struct DependencyValues: Sendable {
5757
@TaskLocal public static var _current = Self()
58-
@TaskLocal fileprivate static var isSetting = false
58+
@TaskLocal static var isSetting = false
5959
@TaskLocal static var currentDependency = CurrentDependency()
6060

6161
private var cachedValues = CachedValues()

0 commit comments

Comments
 (0)