Improvement to support Non-Dependencies Environment inside ReducerProtocol implementation.
We need to find a way how we can do mock on the fly since it only possible when using Dependencies, not with non-dependencies Environment.
✅ Solution: We are improve our TestStore to have capability to hold our Environment, and giving access to developer to change it on the fly
let testStore = TestStore(
initialState: "",
environment: AuthenticatorService.mock,
reducer: Authenticator.init(authenticatorService:)
)
/// here we can mock our environment
////
testStore.environment.getAuthResult = {
.success("success")
}
The reducer implementation will be like this:
struct Authenticator: ReducerProtocol {
typealias State = String
typealias Action = Void
/// ✅ here our Environment
///
internal var authenticatorService: AuthenticatorService
func reduce(into state: inout String, action: Void) -> Effect<Void> {
switch authenticatorService.getAuthResult() {
case let .success(successMessage):
state = successMessage
case let .failure(failureData):
state = failureData.message
}
return .none
}
}
For mocking Example we can do it like this:
Bootstrap.mock(
for: Authenticator(authenticatorService: .mockFailed) // you can mock directly the Environment
) { currentReducer in
... your mocking Dependencies logic
}
Improvement to support Non-Dependencies Environment inside ReducerProtocol implementation. We need to find a way how we can do mock on the fly since it only possible when using Dependencies, not with non-dependencies Environment.
✅ Solution: We are improve our TestStore to have capability to hold our Environment, and giving access to developer to change it on the fly
The reducer implementation will be like this:
For mocking Example we can do it like this: