Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active February 20, 2023 11:40
Show Gist options
  • Save mono0926/84e8a7011c08bdefcc97128a3c9144a9 to your computer and use it in GitHub Desktop.
Save mono0926/84e8a7011c08bdefcc97128a3c9144a9 to your computer and use it in GitHub Desktop.
// https://pub.dev/packages/clock
import 'package:clock/clock.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';
final clockProvider = Provider((ref) => const Clock());
final someTimeService = Provider(SomeTimeService.new);
class SomeTimeService {
SomeTimeService(this._ref);
final Ref _ref;
static final _format = DateFormat();
String get formattedNow => _format.format(_ref.read(clockProvider).now());
}
void main() {
test('today Clock.fixed()', () async {
final container = ProviderContainer(
overrides: [
clockProvider.overrideWithValue(Clock.fixed(DateTime(1986, 9, 26))),
],
);
final service = container.read(someTimeService);
expect(service.formattedNow, 'September 26, 1986 12:00:00 AM');
await Future<void>.delayed(const Duration(seconds: 1));
expect(service.formattedNow, 'September 26, 1986 12:00:00 AM');
});
test('today Clock(() => ...)', () {
var now = DateTime(1986, 9, 26);
final container = ProviderContainer(
overrides: [
clockProvider.overrideWithValue(Clock(() => now)),
],
);
final service = container.read(someTimeService);
expect(service.formattedNow, 'September 26, 1986 12:00:00 AM');
now = DateTime(2021, 8, 31);
expect(service.formattedNow, 'August 31, 2021 12:00:00 AM');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment