Skip to content

feat: Support time manipulation in bloc_test with clock/fakeAsync #4048

@fabianbrandscheid

Description

@fabianbrandscheid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions