Skip to content

Commit 5526c8a

Browse files
authored
Fix prepareDependencies when using TestDependencyKey. (#315)
* Fix prepareDependencies when using TestDependencyKey. * wip * wip
1 parent 7d2eb4a commit 5526c8a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Sources/Dependencies/DependencyValues.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,12 @@ public final class CachedValues: @unchecked Sendable {
477477
return withIssueContext(fileID: fileID, filePath: filePath, line: line, column: column) {
478478
let cacheKey = CacheKey(id: TypeIdentifier(key), context: context)
479479
#if DEBUG
480-
if context == .live, !DependencyValues.isSetting, !(key is any DependencyKey.Type) {
480+
if
481+
context == .live,
482+
!DependencyValues.isSetting,
483+
!(cached[cacheKey] != nil && cached[cacheKey]?.preparationID != nil),
484+
!(key is any DependencyKey.Type)
485+
{
481486
reportIssue(
482487
{
483488
var dependencyDescription = ""

Tests/DependenciesTests/DependencyValuesTests.swift

+12
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,18 @@ final class DependencyValuesTests: XCTestCase {
736736
XCTAssertEqual(now, Date(timeIntervalSinceReferenceDate: 0))
737737
}
738738

739+
func testPrepareDependencies_setsDependency_LiveContext() {
740+
withDependencies {
741+
$0.context = .live
742+
} operation: {
743+
prepareDependencies {
744+
$0[ClientWithEndpoint.self] = ClientWithEndpoint(get: { 1729 })
745+
}
746+
@Dependency(ClientWithEndpoint.self) var client
747+
XCTAssertEqual(client.get(), 1729)
748+
}
749+
}
750+
739751
#if DEBUG && !os(Linux) && !os(WASI) && !os(Windows)
740752
func testPrepareDependencies_MultiplePreparesWithNoAccessBetween() {
741753
prepareDependencies {

0 commit comments

Comments
 (0)