provider icon indicating copy to clipboard operation
provider copied to clipboard

Problem with clock package for mocking time and lazy creation of providers

Open J3012B opened this issue 2 years ago • 2 comments

Describe the bug Using the clock package and its Zone-scoped withClock (https://api.flutter.dev/flutter/package-clock_clock/withClock.html) for automated testing purposes, I've noticed that there seems to occur a problem when creating a Provider lazily via setting lazy: true. When faking the time with clock.now() apparently the delayed creation of the Provider does not mock the faked time, but instead using the actual current time.

To Reproduce

Create a Provider lazily and try to fake the time within it via the clock package (https://pub.dev/packages/clock).

Expected behavior The faked time should be also working within the Provider creation. Faking the time for testing purposes should be possible while using Provider.

Code

withClock(fakeClock, () {
...
BlocProvider<MyCubit>(
            lazy: true,
            create: (context) {
              print("time is ${clock.now()}"); // <== This is not printing the faked time of fakeClock, but the actual current time
              return MyCubit();
            },
          ),
...
});

Note: This example showcases a use of the package flutter_bloc which makes use of the InehritedProvider internally.

J3012B avatar May 10 '22 17:05 J3012B

Is this lazy creation maybe working in a different Zone and therefore this problem occurs?

J3012B avatar May 10 '22 17:05 J3012B

I don't think that's something which can be changed

I'd suggest having the withClock inside create inside.

rrousselGit avatar May 22 '22 09:05 rrousselGit

Not much we can do, so I'll close this

rrousselGit avatar Nov 27 '22 14:11 rrousselGit