receive(_:timeout:assert:fileID:file:line:column:)
Asserts an action was received from an effect and asserts how the state changes.
@MainActor func receive(_ expectedAction: Action, timeout nanoseconds: UInt64? = nil, assert updateStateToExpectedResult: ((_ state: inout State) throws -> Void)? = nil, fileID: StaticString = #fileID, file filePath: StaticString = #filePath, line: UInt = #line, column: UInt = #column) async
Parameters
- expectedAction
An action expected from an effect.
- nanoseconds
The amount of time to wait for the expected action.
- updateStateToExpectedResult
A closure that asserts state changed by sending the action to the store. The mutable state sent to this closure must be modified to match the state of the store after processing the given action. Do not provide a closure if no change is expected.
- fileID
The fileID.
- filePath
The filePath.
- line
The line.
- column
The column.
When an effect is executed in your feature and sends an action back into the system, you can use this method to assert that fact, and further assert how state changes after the effect action is received:
await store.send(.buttonTapped)
await store.receive(.response(.success(42)) {
$0.count = 42
}
Due to the variability of concurrency in Swift, sometimes a small amount of time needs to pass before effects execute and send actions, and that is why this method suspends. The default time waited is very small, and typically it is enough so you should be controlling your dependencies so that they do not wait for real world time to pass (see Dependencies for more information on how to do that).
To change the amount of time this method waits for an action, pass an explicit timeout
argument, or set the timeout
on the TestStore
.