Skip to content

seaneagan/clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clock pub package Build Status

The clock package provides testable replacements for the system clock APIs (new DateTime.now() and new Stopwatch()).

##Usage

Access now and getStopwatch() instead of new DateTime.now() and new Stopwatch(). You may want to import with a prefix:

import 'package:clock/clock.dart' as clock;

int get currentYear => clock.now.year;

Duration timeAction(action()) {
  var stopwatch = clock.getStopwatch()..start();
  action();
  return stopwatch.elapsed;
}

Then use withClock(new Clock(...), () { /* test code */ }); to make your tests deterministic:

import 'package:clock/clock.dart';
import 'package:unittest/unittest.dart';

import 'time_utils.dart'; // What we're testing.

main() {
  group('time utils', () {
  
    test('currentYear should return the current year', () {
      withClock(new Clock(initialTime: new DateTime(2014, 3, 6)), () {
        expect(currentYear, 2014);
      });
    });

    test('timeAction should time the action', () {
      var elapsed = Duration.ZERO;
      
      withClock(new Clock(elapsed: () => elapsed), () {
        expect(timeAction(() {
          elapsed += const Duration(seconds: 5);
        }), const Duration(seconds: 5));
      });
    });
  });
}

About

Testable replacements for dart system clock APIs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages