@@ -568,8 +568,9 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
568
568
/// - initialState: The state the feature starts in.
569
569
/// - reducer: The reducer that powers the runtime of the feature.
570
570
public init < Reducer: ReducerProtocol > (
571
- initialState: State ,
571
+ initialState: @autoclosure ( ) -> State ,
572
572
reducer: Reducer ,
573
+ prepareDependencies: ( inout DependencyValues ) -> Void = { _ in } ,
573
574
file: StaticString = #file,
574
575
line: UInt = #line
575
576
)
@@ -580,6 +581,11 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
580
581
Action == ScopedAction ,
581
582
Environment == Void
582
583
{
584
+ var dependencies = DependencyValues ( )
585
+ dependencies. context = . test
586
+ prepareDependencies ( & dependencies)
587
+ let initialState = DependencyValues . $_current. withValue ( dependencies) { initialState ( ) }
588
+
583
589
let reducer = TestReducer ( Reduce ( reducer) , initialState: initialState)
584
590
self . _environment = . init( wrappedValue: ( ) )
585
591
self . file = file
@@ -589,6 +595,7 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
589
595
self . store = Store ( initialState: initialState, reducer: reducer)
590
596
self . timeout = 100 * NSEC_PER_MSEC
591
597
self . toScopedState = { $0 }
598
+ self . dependencies = dependencies
592
599
}
593
600
594
601
@available (
@@ -1037,7 +1044,9 @@ extension TestStore where ScopedState: Equatable {
1037
1044
case . on:
1038
1045
var expectedWhenGivenPreviousState = expected
1039
1046
if let updateStateToExpectedResult = updateStateToExpectedResult {
1040
- try updateStateToExpectedResult ( & expectedWhenGivenPreviousState)
1047
+ try DependencyValues . $_current. withValue ( self . dependencies) {
1048
+ try updateStateToExpectedResult ( & expectedWhenGivenPreviousState)
1049
+ }
1041
1050
}
1042
1051
expected = expectedWhenGivenPreviousState
1043
1052
@@ -1050,7 +1059,9 @@ extension TestStore where ScopedState: Equatable {
1050
1059
case . off:
1051
1060
var expectedWhenGivenActualState = actual
1052
1061
if let updateStateToExpectedResult = updateStateToExpectedResult {
1053
- try updateStateToExpectedResult ( & expectedWhenGivenActualState)
1062
+ try DependencyValues . $_current. withValue ( self . dependencies) {
1063
+ try updateStateToExpectedResult ( & expectedWhenGivenActualState)
1064
+ }
1054
1065
}
1055
1066
expected = expectedWhenGivenActualState
1056
1067
@@ -1062,10 +1073,12 @@ extension TestStore where ScopedState: Equatable {
1062
1073
&& expectedWhenGivenActualState == actual
1063
1074
{
1064
1075
var expectedWhenGivenPreviousState = current
1065
- if let modify = updateStateToExpectedResult {
1076
+ if let updateStateToExpectedResult = updateStateToExpectedResult {
1066
1077
_XCTExpectFailure ( strict: false ) {
1067
1078
do {
1068
- try modify ( & expectedWhenGivenPreviousState)
1079
+ try DependencyValues . $_current. withValue ( self . dependencies) {
1080
+ try updateStateToExpectedResult ( & expectedWhenGivenPreviousState)
1081
+ }
1069
1082
} catch {
1070
1083
XCTFail (
1071
1084
"""
@@ -2063,11 +2076,7 @@ public struct TestStoreTask: Hashable, Sendable {
2063
2076
2064
2077
class TestReducer < State, Action> : ReducerProtocol {
2065
2078
let base : Reduce < State , Action >
2066
- var dependencies = { ( ) -> DependencyValues in
2067
- var dependencies = DependencyValues ( )
2068
- dependencies. context = . test
2069
- return dependencies
2070
- } ( )
2079
+ var dependencies = DependencyValues ( )
2071
2080
let effectDidSubscribe = AsyncStream< Void> . streamWithContinuation( )
2072
2081
var inFlightEffects : Set < LongLivingEffect > = [ ]
2073
2082
var receivedActions : [ ( action: Action , state: State ) ] = [ ]
0 commit comments