Skip to content

Commit 0a6f2b2

Browse files
mbrandonworonbz
andauthored
Fix TestStore.init with prepareDependencies. (pointfreeco#1955)
* Fix TestStore.init with prepareDependencies. * Update Tests/ComposableArchitectureTests/TestStoreTests.swift Co-authored-by: Oron Ben Zvi <[email protected]> * Update Tests/ComposableArchitectureTests/TestStoreTests.swift Co-authored-by: Oron Ben Zvi <[email protected]> --------- Co-authored-by: Oron Ben Zvi <[email protected]>
1 parent a0beb19 commit 0a6f2b2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Sources/ComposableArchitecture/TestStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,8 @@ public final class TestStore<State, Action, ScopedState, ScopedAction, Environme
666666
Environment == Void
667667
{
668668
var dependencies = DependencyValues._current
669-
prepareDependencies(&dependencies)
670669
let initialState = withDependencies {
670+
prepareDependencies(&dependencies)
671671
$0 = dependencies
672672
} operation: {
673673
initialState()

Tests/ComposableArchitectureTests/TestStoreTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ final class TestStoreTests: XCTestCase {
328328
func testOverrideDependenciesOnTestStore_Init() {
329329
struct Counter: ReducerProtocol {
330330
@Dependency(\.calendar) var calendar
331+
@Dependency(\.client.fetch) var fetch
331332
@Dependency(\.locale) var locale
332333
@Dependency(\.timeZone) var timeZone
333334
@Dependency(\.urlSession) var urlSession
334335

335336
func reduce(into state: inout Int, action: Bool) -> EffectTask<Bool> {
336337
_ = self.calendar
338+
_ = self.fetch()
337339
_ = self.locale
338340
_ = self.timeZone
339341
_ = self.urlSession
@@ -347,6 +349,7 @@ final class TestStoreTests: XCTestCase {
347349
reducer: Counter()
348350
) {
349351
$0.calendar = Calendar(identifier: .gregorian)
352+
$0.client.fetch = { 1 }
350353
$0.locale = Locale(identifier: "en_US")
351354
$0.timeZone = TimeZone(secondsFromGMT: 0)!
352355
$0.urlSession = URLSession(configuration: .ephemeral)
@@ -402,3 +405,14 @@ final class TestStoreTests: XCTestCase {
402405
}
403406
}
404407
}
408+
409+
private struct Client: DependencyKey {
410+
var fetch: () -> Int
411+
static let liveValue = Client(fetch: { 42 })
412+
}
413+
extension DependencyValues {
414+
fileprivate var client: Client {
415+
get { self[Client.self] }
416+
set { self[Client.self] = newValue }
417+
}
418+
}

0 commit comments

Comments
 (0)