You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thoughts on allowing efficient overriding of helpers in tests - EXPERIMENTAL
Scenario.
You an application to use a since Clock instance to get the current time, etc.
You want to provide a "mock" of Clock in certain tests to validate related code.
Goals
The mocking solution should have minimal runtime override in production.
Do something leveraging existing Dart language and runtime features.
Make it hard (impossible?) to swap out the mock in a production app.
Solution
Expose top-level "provider" fields that give access to the desired object
with the production value pre-defined.
// See lib/example/package_providers.dartimport'package:override/override.dart';
import'clock.dart';
finalProvider<Clock> clockProvider =newProvider<Clock>(constClock());
Use the provider to access the desired value anywhere/everywhere in your code.
// See 'lib/example/time_utils.dart'import'package_providers.dart';
Durationget timeSinceMidnight {
var now = clockProvider.value.now;
returnnewDuration(
hours: now.hour, minutes: now.minute, seconds: now.second);
}
intget secondsSinceMidnight => timeSinceMidnight.inSeconds;
Override the value in your test code.
// See test/override_test.dart
clockProvider.overrideValue(newRelativeClock(initialTime: dummyTime));
test('Test `timeSinceMidnight` correctly calculates the time since midnight.', () {
// Use `timeSinceMidnight`, but with an overridden implementation of// `Clock`.expect(timeSinceMidnight, constDuration(hours:9));
});