Recipes
Advance policy time deterministically
Test Debounce, Throttle, Rate Limit, and timestamps without sleeping.
v0.1.0-dev.1Intermediateusecase_forge_test
Open source reference ↗Advance policy time deterministically
Inject TestUseCaseClock into the UseCase instead of waiting for wall-clock timers.
dart
final clock = TestUseCaseClock(DateTime.utc(2030));
final useCase = SearchUseCase(clock: clock);
final observer = UseCaseTestObserver<SearchState>(useCase);
useCase.add(const SearchCommand('dart'));
clock.advance(const Duration(milliseconds: 300));
await observer.waitForHistory(hasLength(1));
Pass the clock to the core constructor with super(initialState: ..., clock: clock). advance runs due policy callbacks and controls Entry timestamps. The test now states the exact boundary it crosses and does not depend on scheduler or machine speed.
Close the UseCase and cancel the observer in teardown. Advancing time does not automatically prove terminal completion; wait for the observable condition that the scenario requires.