Skip to content

Commit 27691a7

Browse files
umbertovoltamluisbrown
authored andcommitted
[Fix] Check the state does not actually change when no changes are expected (#1161)
* fix: check the state doesn’t change when the modify closure is nil * test: add tests for the 'unexpected changes on the state' failure
1 parent 7d9650f commit 27691a7

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

Sources/ComposableArchitecture/TestSupport/TestStore.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@
398398
file: StaticString,
399399
line: UInt
400400
) throws {
401-
guard let modify = modify else { return }
402401
let current = expected
403-
try modify(&expected)
402+
if let modify = modify {
403+
try modify(&expected)
404+
}
404405

405406
if expected != actual {
406407
let difference =
@@ -414,16 +415,19 @@
414415
\(String(describing: actual).indent(by: 2))
415416
"""
416417

418+
let messageHeading = modify != nil ?
419+
"A state change does not match expectation" :
420+
"State was not expected to change, but a change occurred"
417421
XCTFail(
418422
"""
419-
A state change does not match expectation: …
423+
\(messageHeading): …
420424
421425
\(difference)
422426
""",
423427
file: file,
424428
line: line
425429
)
426-
} else if expected == current {
430+
} else if expected == current && modify != nil {
427431
XCTFail(
428432
"""
429433
Expected state to change, but no change occurred.

Tests/ComposableArchitectureTests/TestStoreFailureTests.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,59 @@ class TestStoreFailureTests: XCTestCase {
6161
}
6262
}
6363

64+
func testUnexpectedStateChangeOnSendFailure() {
65+
struct State: Equatable { var count = 0 }
66+
let store = TestStore(
67+
initialState: .init(),
68+
reducer: Reducer<State, Void, Void> { state, action, _ in state.count += 1
69+
return .none
70+
},
71+
environment: ()
72+
)
73+
74+
XCTExpectFailure {
75+
store.send(())
76+
} issueMatcher: {
77+
$0.compactDescription == """
78+
State was not expected to change, but a change occurred: …
79+
80+
− TestStoreFailureTests.State(count: 0)
81+
+ TestStoreFailureTests.State(count: 1)
82+
83+
(Expected: −, Actual: +)
84+
"""
85+
}
86+
}
87+
88+
func testUnexpectedStateChangeOnReceiveFailure() {
89+
struct State: Equatable { var count = 0 }
90+
enum Action { case first, second }
91+
let store = TestStore(
92+
initialState: .init(),
93+
reducer: Reducer<State, Action, Void> { state, action, _ in
94+
switch action {
95+
case .first: return .init(value: .second)
96+
case .second: state.count += 1; return .none
97+
}
98+
},
99+
environment: ()
100+
)
101+
102+
store.send(.first)
103+
XCTExpectFailure {
104+
store.receive(.second)
105+
} issueMatcher: {
106+
$0.compactDescription == """
107+
State was not expected to change, but a change occurred: …
108+
109+
− TestStoreFailureTests.State(count: 0)
110+
+ TestStoreFailureTests.State(count: 1)
111+
112+
(Expected: −, Actual: +)
113+
"""
114+
}
115+
}
116+
64117
func testReceivedActionAfterDeinit() {
65118
XCTExpectFailure {
66119
do {

0 commit comments

Comments
 (0)