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