Overriding TestStore dependencies not working #1888
-
Hi, have a question/problem trying to override my reducer dependencies via TestStore. First of all this is the way dependencies are injected into my component, is a bit weird since there are some peculiarities. public final class MyComponent {
let provider: DependencyProvider
private static var staticProvider: DependencyProvider!
public init(with provider: DependencyProvider) {
self.provider = provider
Self.dependenciesProvider = provider
}
}
extension MyComponent {
enum AnExternalDependencyServiceKey: DependencyKey {
static var liveValue: AnExternalDependencyService { staticProvider.externalDependencySecret }
}
// More dependencies...
}
extension DependencyValues {
var anExternalDependencyService: AnExternalDependencyService {
get { self[AuthExperience.AnExternalDependencyServiceKey.self] }
set { self[AuthExperience.AnExternalDependencyServiceKey.self] = newValue }
}
// More dependencies...
} Then are consumed into my reducer normally struct MyReducer: ReducerProtocol {
@Dependency(\.anExternalDependencyService) var anExternalDependencyService
// More code and use of the dependency works great
} Now, when writing my reducer unit tests, I do this private let initialState: MyReducer.State = .init()
private var reducer: MyReducer!
override func setUp() {
super.setUp()
reducer = MyReducer()
}
func testSomething() async {
let store = TestStore(initialState: self.initialState, reducer: self.reducer) {
$0.anExternalDependencyService = AnExternalDependencyServiceMock()
}
// My assertions...
} But test crashes with this error
If I try this approach, it fails too with the same message func testSomething() async {
let store = TestStore(initialState: self.initialState, reducer: self.reducer)
store.dependencies.anExternalDependencyService = AnExternalDependencyServiceMock()
// My assertions...
} And that's what I'm actually doing, right? Is there something wrong with my assumption or code? Have to use Thanks for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Hey @WedgeSparda, it is possible that you're hosting your tests with the main app, and that this app is creating a store that accesses the dependencies, causing this kind of failure. Otherwise, I'm not sure what's happening regarding the Having the reducer stored as a property in your tests is a little odd too, but I guess you have your reasons to do so, and this is likely not what's causing the issue. |
Beta Was this translation helpful? Give feedback.
-
Hi @WedgeSparda, can you share a project that reproduces the problem? |
Beta Was this translation helpful? Give feedback.
Hi @WedgeSparda, can you share a project that reproduces the problem?