Skip to content

Commit 8282b0c

Browse files
authored
Test values should default to preview before live (#30)
If you define a live value and preview value, we should probably default to the preview value in tests to avoid accidentally hitting the live implementation. The test will fail when the dependency is accessed regardless.
1 parent 6fb0871 commit 8282b0c

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

Sources/Dependencies/DependencyKey.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ extension DependencyKey {
163163
/// which will take precedence over this implementation.
164164
public static var previewValue: Value { Self.liveValue }
165165

166-
/// A default implementation that provides the ``liveValue`` to XCTest runs, but will trigger test
167-
/// failure to occur if accessed.
166+
/// A default implementation that provides the ``previewValue`` to XCTest runs (or ``liveValue``,
167+
/// if no preview value is implemented), but will trigger a test failure when accessed.
168168
///
169169
/// To prevent test failures, explicitly override the dependency in any tests in which it is
170170
/// accessed:
@@ -183,7 +183,7 @@ extension DependencyKey {
183183
/// which will take precedence over this implementation.
184184
public static var testValue: Value {
185185
guard !DependencyValues.isSetting
186-
else { return Self.liveValue }
186+
else { return Self.previewValue }
187187

188188
var dependencyDescription = ""
189189
if let fileID = DependencyValues.currentDependency.fileID,
@@ -228,7 +228,7 @@ extension DependencyKey {
228228
requirement of the 'DependencyKey' protocol.
229229
"""
230230
)
231-
return Self.liveValue
231+
return Self.previewValue
232232
}
233233
}
234234

Tests/DependenciesTests/DependencyKeyTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ final class DependencyKeyTests: XCTestCase {
8888

8989
#if DEBUG && !os(Linux)
9090
XCTExpectFailure {
91-
XCTAssertEqual(42, Key.testValue)
91+
XCTAssertEqual(1729, Key.testValue)
9292
} issueMatcher: { issue in
9393
issue.compactDescription == """
9494
A dependency has no test implementation, but was accessed from a test context:

0 commit comments

Comments
 (0)