Skip to content

Commit

Permalink
Test values should default to preview before live (#30)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephencelis authored Jan 19, 2023
1 parent 6fb0871 commit 8282b0c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Sources/Dependencies/DependencyKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ extension DependencyKey {
/// which will take precedence over this implementation.
public static var previewValue: Value { Self.liveValue }

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

var dependencyDescription = ""
if let fileID = DependencyValues.currentDependency.fileID,
Expand Down Expand Up @@ -228,7 +228,7 @@ extension DependencyKey {
requirement of the 'DependencyKey' protocol.
"""
)
return Self.liveValue
return Self.previewValue
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/DependenciesTests/DependencyKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final class DependencyKeyTests: XCTestCase {

#if DEBUG && !os(Linux)
XCTExpectFailure {
XCTAssertEqual(42, Key.testValue)
XCTAssertEqual(1729, Key.testValue)
} issueMatcher: { issue in
issue.compactDescription == """
A dependency has no test implementation, but was accessed from a test context:
Expand Down

0 comments on commit 8282b0c

Please sign in to comment.