Skip to content

Commit 269d327

Browse files
committed
Add MockCommand state control explanation to docs
Add detailed section explaining the difference between automatic and manual state control in MockCommand: - Explain that run() automatically toggles isRunning synchronously - Show why manual control methods are better for testing states - Provide clear examples for both approaches - Add guidance on when to use each approach This clarifies a common confusion point about MockCommand behavior.
1 parent 72581f2 commit 269d327

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

docs/documentation/command_it/testing.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,60 @@ For testing code that depends on commands, use the built-in `MockCommand` class
152152
- **`endRunWithError(String message)`** - Complete execution with an error
153153
- **`runCount`** - Track how many times the command was run
154154

155+
### Automatic vs Manual State Control
156+
157+
**Important:** MockCommand's `run()` method **automatically toggles `isRunning`**, but it happens **synchronously**:
158+
159+
```dart
160+
// When you call run():
161+
mockCommand.run('param');
162+
// isRunning goes: false → true → false (instantly)
163+
```
164+
165+
This synchronous toggle means you typically won't catch the `true` state in tests. For testing state transitions, use the **manual control methods**:
166+
167+
**Manual Control (Recommended for Testing):**
168+
169+
```dart
170+
final mockCommand = MockCommand<String, String>(initialValue: '');
171+
172+
// You control when state changes
173+
mockCommand.startRun('param'); // isRunning = true
174+
expect(mockCommand.isRunning.value, true); // ✅ Can verify loading state
175+
176+
// Later, complete the operation
177+
mockCommand.endRunWithData('result'); // isRunning = false
178+
expect(mockCommand.isRunning.value, false); // ✅ Can verify completed state
179+
```
180+
181+
**Automatic via run() (Quick Fire-and-Forget):**
182+
183+
```dart
184+
final mockCommand = MockCommand<String, String>(initialValue: '');
185+
186+
// Queue results first
187+
mockCommand.queueResultsForNextRunCall([
188+
CommandResult('param', 'result', null, false),
189+
]);
190+
191+
// Then run - isRunning briefly true, then immediately false
192+
mockCommand.run('param');
193+
194+
// isRunning is already false by now (synchronous)
195+
expect(mockCommand.isRunning.value, false);
196+
```
197+
198+
**Use manual control methods when:**
199+
- Testing loading/running state UI
200+
- Verifying state transitions in sequence
201+
- Testing error state handling
202+
- Simulating long-running operations
203+
204+
**Use `run()` + `queueResultsForNextRunCall()` when:**
205+
- You only care about the final result
206+
- Testing simple success/error outcomes
207+
- You don't need to verify intermediate states
208+
155209
**This pattern demonstrates:**
156210

157211
<ul style="list-style: none; padding-left: 0;">

0 commit comments

Comments
 (0)