Skip to content
Cyrille DUPUYDAUBY edited this page Jan 31, 2021 · 7 revisions

Equality

IsEqualToIgnoringMilliseconds(expected)

Checks that the sut is equal to the expected DateTime, disregarding milliseconds.

var sut = new DateTime(2018, 2, 6, 9, 18, 12, 152);
// This check succeeds
Check.That(sut).IsEqualToIgnoringMilliseconds(new DateTime(2018, 2, 6, 9, 18, 12, 870));
// This check fails
Check.That(sut).IsEqualToIgnoringMilliseconds(new DateTime(2018, 2, 6, 9, 18, 13, 152));

IsEqualToIgnoringSeconds(expected)

Checks that the sut is equal to the expected DateTime, disregarding seconds.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsEqualToIgnoringSeconds(new DateTime(2018, 2, 6, 9, 18, 15));
// This check fails
Check.That(sut).IsEqualToIgnoringSeconds(new DateTime(2018, 2, 6, 9, 17, 12));

IsEqualToIgnoringMinutes(expected)

Checks that the sut is equal to the expected DateTime, disregarding minutes.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsEqualToIgnoringMinutes(new DateTime(2018, 2, 6, 9, 17, 15));
// This check fails
Check.That(sut).IsEqualToIgnoringMinutes(new DateTime(2018, 2, 6, 8, 18, 12));

IsEqualToIgnoringHours(expected)

Checks that the sut is equal to the expected DateTime, disregarding minutes.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsEqualToIgnoringHours(new DateTime(2018, 2, 6, 8, 17, 15));
// This check fails
Check.That(sut).IsEqualToIgnoringHours(new DateTime(2018, 2, 7, 9, 18, 12));

IsSameDayAs(expected)

Checks that sut has the same month as expected; fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsSameDayAs(new DateTime(2014, 11, 6, 8, 17, 15));
// This check fails
Check.That(sut).IsSameDayAs(new DateTime(2018, 2, 7, 9, 18, 12));

IsSameMonthAs(expected)

Checks that sut has the same day of month as expected; fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsSameMonthAs(new DateTime(2014, 2, 12, 8, 17, 15));
// This check fails
Check.That(sut).IsSameMonthAs(new DateTime(2018, 6, 7, 9, 18, 12));

IsSameYearAs(expected)

Checks that sut has the same year as expected; fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsSameYearAs(new DateTime(2018, 11, 6, 8, 17, 15));
// This check fails
Check.That(sut).IsSameYearAs(new DateTime(2014, 2, 7, 9, 18, 12));

IsCloseTo(expected, tolerance)

Checks that sut is close to the expected date time, within +/- tolerance. The tolerance can be a Timespan or a Duration.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsCloseTo(new DateTime(2018, 2, 6, 9, 18, 15), new Duration(5, TimeUnit.Minutes));
// This check succeeds
Check.That(sut).IsCloseTo(new DateTime(2018, 2, 6, 9, 18, 15), TimeSpan.FromMinutes(5));
// This check fails
Check.That(sut).IsCloseTo(new DateTime(2018, 2, 6, 9, 18, 02), new Duration(5, TimeUnit.Minutes));

Comparison

IsBefore(reference)

Checks that the sut represents a date time earlier than reference, fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsBefore(new DateTime(2018, 11, 6, 8, 17, 15);
// This check fails
Check.That(sut).IsBefore(new DateTime(2014, 2, 5, 9, 18, 12);

IsAfter(reference)

Checks that the sut represents a date time later than reference, fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// This check succeeds
Check.That(sut).IsAfter(new DateTime(2018, 2, 5, 8, 17, 15);
// This check fails
Check.That(sut).IsAfter(new DateTime(2014, 2, 8, 9, 18, 12);

IsBeforeOrEqualTo(reference)

Checks that the sut represents a date time earlier than reference, fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// Those checks succeed
Check.That(sut).IsBeforeOrEqualTo(new DateTime(2018, 11, 6, 8, 17, 15);
Check.That(sut).IsBeforeOrEqualTo(new DateTime(018, 2, 6, 9, 18, 12);
// This check fails
Check.That(sut).IsBeforeOrEqualTo(new DateTime(2014, 2, 5, 9, 18, 12);

IsAfterOrEqualTo(reference)

Checks that the sut represents a date time later than reference, fails otherwise.

var sut = new DateTime(2018, 2, 6, 9, 18, 12);
// Those checks succeed
Check.That(sut).IsAfterOrEqualTo(new DateTime(2018, 2, 5, 8, 17, 15);
Check.That(sut).IsAfterOrEqualTo(new DateTime(2018, 2, 6, 9, 18, 12);
// This check fails
Check.That(sut).IsAfterOrEqualTo(new DateTime(2014, 2, 8, 9, 18, 12);
Clone this wiki locally