felangel / bloc

A predictable state management library that helps implement the BLoC design pattern

Home Page:https://bloclibrary.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat: Support time manipulation in bloc_test with clock/fakeAsync

fabianbrandscheid opened this issue · comments

Description

I have a block which executes a network access via a usecase. It waits 20 seconds for a response before the future throws a timeout and a FailureState is emitted.
I wanted to use the Clock&FakeAsync package to avoid having to wait 20 seconds in the BlocTest in the event of an error.
For "manual" Bloc tests, the entire test content can simply be wrapped:

 test("Future.timeout() throws an error once the timeout is up", () async {
    // Act

    // Assert
    unawaited(fakeAsync((async) async {
      await setUpGetSyncSntity(duration: const Duration(milliseconds: 22));

      final bloc = getBloc();

      bloc.add(SyncStartEvent());

      async.elapse(Duration(seconds: 21));

      await expectLater(
          bloc.stream,
          emitsInOrder([
            const State1(),
            const TimeOutState(),
          ]));

      // Clean up
      await bloc.close();
    }));
  });

This advances the time by 21 seconds and simulates a timeout without actually having to wait 21 seconds.

Desired Solution

Could this option also be supported for BlocTests?
I suspect that the complete content of testBloc would have to be wrapped internally with FakeAsync and the time would have to be fast-forwarded after the call of act

Hi, there is already opened PR #3796

Oh thanks, I will close my issue