Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

clock pub packageBuild 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;
intget currentYear => clock.now.year;
DurationtimeAction(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(newClock(initialTime:newDateTime(2014, 3, 6)), () {
expect(currentYear, 2014);
});
});
test('timeAction should time the action', () {
var elapsed =Duration.ZERO;
withClock(newClock(elapsed: () => elapsed), () {
expect(timeAction(() {
elapsed +=constDuration(seconds:5);
}), constDuration(seconds:5));
});
});
});
}

About

Testable replacements for dart system clock APIs

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages