manual_clock 1.3.3 copy "manual_clock: ^1.3.3" to clipboard
manual_clock: ^1.3.3 copied to clipboard

A package for testing current time related code.

Manual Clock #

This package is for testing current time and Timer related code.

The ManualClock class is a derived class of Clock.

With clock and fake_async libraries, you can test current time related code by using clock.now(). But inside fake async callback, you can't use await. Because of this restriction, Sometimes it is hard to test your code. With this library, you can test current time and Timer related code using await.

Restrictions #

  • Periodic timers of 0 duration are not supported.
  • One-shot timers of 0 duration are supported.
  • If a target code contains a code like await Future.delayed(...), it will stop there permanently.

Getting started #

pubspec.yaml

dev_dependencies:
  manual_clock: ^latest_version

Usage #

test('a test case', () async {
  await withManualClock((clock) async {
    final obj = ClassDependOnNow(...);
    clock.elapse(Duration(...));

    // You can use await inside callback
    final result = await obj.getResult();
    expect(result, ...);

    // You can test Timers
    var fired = false;
    Timer(duration, () { fired = true; });
    clock.elapse(duration);
    expect(fired, true);

    // If you need to wait for real life time inside callback,
    // use waitRealLifeTime(duration)
    await waitRealLifeTime(Duration(seconds: 10));
  },
  // optionally set initial time
  initialTime: DateTime(2000, 1, 1));
});
1
likes
150
points
31
downloads

Publisher

verified publishersousyokunotomonokai.com

Weekly Downloads

A package for testing current time related code.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

clock, collection

More

Packages that depend on manual_clock