Skip to main content

Notifications

MockTimeSystem.On lets a test observe time-related events from the system under test, which is useful for asserting "the code asked to sleep for 5 seconds" without relying on side effects.

MockTimeSystem timeSystem = new();

using IAwaitableCallback<TimeSpan> sleeps = timeSystem.On.ThreadSleep();

new Gallery(timeSystem).CloseForTheNight(); // waits 5 seconds before switching off the lights

TimeSpan[] requestedSleeps = sleeps.Wait(count: 1);
await That(requestedSleeps[0]).IsEqualTo(TimeSpan.FromSeconds(5));

Like the file-system notifications, each method returns an IAwaitableCallback<T> you dispose to unsubscribe and Wait/WaitAsync to block until enough events arrive.

Available callbacks​

Method on OnFires whenT
DateTimeRead()DateTime.Now, UtcNow or Today is readDateTime
DateTimeOffsetRead()DateTimeOffset.Now or UtcNow is readDateTimeOffset
TaskDelay()Task.Delay (any overload) is calledTimeSpan
ThreadSleep()Thread.Sleep (any overload) is calledTimeSpan
TimeChanged()the simulated time advanced (auto-advance or manual)DateTime
PeriodicTimer.WaitingForNextTick()a PeriodicTimer is waiting for its next tick (.NET 8 or later)IPeriodicTimer

Each accepts an optional callback (run synchronously when the event fires) and an optional predicate to filter events before they are recorded.

// Only record sleeps longer than 1 second
using IAwaitableCallback<TimeSpan> longSleeps = timeSystem.On.ThreadSleep(
predicate: ts => ts > TimeSpan.FromSeconds(1));