Replies: 1 comment 4 replies
-
Hi @Pomanks, I'm not able to reproduce this behavior with your code. I copied-pasted it into a project and added the missing pieces necessary to get it compiling, and the test behaves as I would expect: import ComposableArchitecture
import Testing
@Suite
struct TestSuite {
@Test
@MainActor
func someTest() async throws {
let store = TestStore(initialState: .init()) {
MainFeature()
}
// default result
await store.send(\.operation.someAction)
await store.receive(\.endpointResult) {
$0.text = "foo bar"
}
store.dependencies[APIClient.self].someEndpointCalled = { @Sendable in
"foo"
}
// first call
await store.send(\.operation.someAction)
await store.receive(\.endpointResult) {
$0.text = "foo" //👈 This is "foo", not "foo bar"
}
// second call
await store.send(\.operation.someAction)
await store.receive(\.endpointResult) // 👈 No assertion necessary because state didn't change
}
}
@Reducer
struct MainFeature: Sendable {
@ObservableState
struct State: Equatable, Sendable {
var text: String = ""
}
enum Action: Sendable {
case operation(OperationAction)
case endpointResult(String)
@CasePathable
enum OperationAction: Sendable {
case someAction
}
}
@Dependency(APIClient.self)
var apiClient
var body: some ReducerOf<Self> {
Reduce<State, Action> { state, action in
switch action {
case .operation:
break
case let .endpointResult(string):
state.text = string
}
return .none
}
OperationLogic()
}
}
@Reducer
struct OperationLogic: Sendable {
@Dependency(APIClient.self)
var apiClient
var body: some ReducerOf<MainFeature> {
Reduce<State, Action> { _, action in
guard case let .operation(operationAction) = action else {
return .none
}
switch operationAction {
case .someAction:
return .run { send in
let response = await apiClient.someEndpointCalled()
await send(.endpointResult(response))
}
}
}
}
}
struct APIClient: TestDependencyKey {
var someEndpointCalled: @Sendable () async -> String
static let testValue = APIClient { "foo bar" }
} There must be something else happening in your project. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! 👋🏻
I’m struggling a bit with tests and dependencies override in a peculiar context:
I have some
MainFeature
reducer which implements a separate reducer calledOperationLogic
to separate some actions/state from it, without having to create a brand new feature for that.The implementation looks a bit like that: (I took inspiration from isowords’s game and isowords’s turn based by the way)
As you can see, I’m not using
Scope(state:action:)
inside theMainFeature
’sbody
since it relies on the sameAction
/State
.The problem is, in my tests, when I override the
apiClient
’ssomeEndpointCall()
to return something else than it’s default result, usingstore.dependencies
the result only seem to be overridden on the second call, like so:I’m really wondering if it's because of my current implementation (and not using
Scope
)I’m setting up an example project to but until then if you have any idea that comes to mind, I’m all ears 😄
Beta Was this translation helpful? Give feedback.
All reactions