provider
provider copied to clipboard
Problem with clock package for mocking time and lazy creation of providers
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.
Is this lazy creation maybe working in a different Zone
and therefore this problem occurs?
I don't think that's something which can be changed
I'd suggest having the withClock inside create
inside.
Not much we can do, so I'll close this