Skip to content

Fix TestStore.init with prepareDependencies. #1955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ComposableArchitecture/TestStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,8 @@ public final class TestStore<State, Action, ScopedState, ScopedAction, Environme
Environment == Void
{
var dependencies = DependencyValues._current
prepareDependencies(&dependencies)
let initialState = withDependencies {
prepareDependencies(&dependencies)
$0 = dependencies
} operation: {
initialState()
Expand Down
14 changes: 14 additions & 0 deletions Tests/ComposableArchitectureTests/TestStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,14 @@ final class TestStoreTests: XCTestCase {
func testOverrideDependenciesOnTestStore_Init() {
struct Counter: ReducerProtocol {
@Dependency(\.calendar) var calendar
@Dependency(\.client.fetch) var fetch
@Dependency(\.locale) var locale
@Dependency(\.timeZone) var timeZone
@Dependency(\.urlSession) var urlSession

func reduce(into state: inout Int, action: Bool) -> EffectTask<Bool> {
_ = self.calendar
_ = self.fetch()
_ = self.locale
_ = self.timeZone
_ = self.urlSession
Expand All @@ -347,6 +349,7 @@ final class TestStoreTests: XCTestCase {
reducer: Counter()
) {
$0.calendar = Calendar(identifier: .gregorian)
$0.client.fetch = { 1 }
$0.locale = Locale(identifier: "en_US")
$0.timeZone = TimeZone(secondsFromGMT: 0)!
$0.urlSession = URLSession(configuration: .ephemeral)
Expand Down Expand Up @@ -402,3 +405,14 @@ final class TestStoreTests: XCTestCase {
}
}
}

private struct Client: DependencyKey {
var fetch: () -> Int
static let liveValue = Client(fetch: { 42 })
}
extension DependencyValues {
fileprivate var client: Client {
get { self[Client.self] }
set { self[Client.self] = newValue }
}
}