Skip to main content

TimeSpan

Describes the possible expectations for TimeSpan.

Equality

You can verify, that the TimeSpan is equal to another one or not:

TimeSpan subject = TimeSpan.FromSeconds(42);

await Expect.That(subject).IsEqualTo(TimeSpan.FromSeconds(42));
await Expect.That(subject).IsNotEqualTo(TimeSpan.FromSeconds(43));

You can also specify a tolerance:

TimeSpan subject = TimeSpan.FromSeconds(42);

await Expect.That(subject).IsEqualTo(TimeSpan.FromSeconds(43)).Within(TimeSpan.FromSeconds(1))
.Because("we accept values between 0:41 and 0:43");

Greater than

You can verify, that the TimeSpan is greater than (or equal to) another number:

TimeSpan subject = TimeSpan.FromSeconds(42);

await Expect.That(subject).IsGreaterThan(TimeSpan.FromSeconds(41));
await Expect.That(subject).IsGreaterThanOrEqualTo(TimeSpan.FromSeconds(42));

You can also specify a tolerance:

TimeSpan subject = TimeSpan.FromSeconds(41);

await Expect.That(subject).IsGreaterThan(42).Within(TimeSpan.FromSeconds(2))
.Because("we accept values greater than 0:40 (0:42 ± 2s)");

Less than

You can verify, that the TimeSpan is less than (or equal to) another number:

TimeSpan subject = TimeSpan.FromSeconds(42);

await Expect.That(subject).IsLessThan(TimeSpan.FromSeconds(43));
await Expect.That(subject).IsLessThanOrEqualTo(TimeSpan.FromSeconds(42));

You can also specify a tolerance:

TimeSpan subject = TimeSpan.FromSeconds(43);

await Expect.That(subject).IsLessThan(42).Within(TimeSpan.FromSeconds(2))
.Because("we accept values less than 0:44 (0:42 ± 2s)");

Positive / negative

You can verify, that the TimeSpan is positive or negative:

await Expect.That(TimeSpan.FromSeconds(42)).IsPositive();
await Expect.That(TimeSpan.FromSeconds(-3)).IsNegative();