Skip to content

DateTime Should().BeLessThan(...).After(b) not check After #1165

@SvetlanaNesterova

Description

@SvetlanaNesterova

Hello!

It seems strange for me that DateTimeAssertions works in this way:

[Test]
public void Test_before_and_after()
{
    var a = new DateTime(2019, 01, 01, 12, 0, 0, DateTimeKind.Utc);
    var b = new DateTime(2019, 01, 01, 12, 30, 0, DateTimeKind.Utc);
    a.Should().BeLessThan(1.Hours()).Before(b);
    a.Should().BeLessThan(1.Hours()).After(b);
}

These methods check that difference is less than value, and it is true when difference is negative. But is not correct to say that a is less than an hour after b when a - b == -30.Minutes()

I think there should be assertion that difference (TimeSpan actual) should be not negative in methods DateTimeRangeAssertions.Before and DateTimeRangeAssertions.After.

TimeSpan actual = target - subject.Value;
if (!predicate.IsMatchedBy(actual, timeSpan))
{
Execute.Assertion
.BecauseOf(because, becauseArgs)

TimeSpan actual = subject.Value - target;
if (!predicate.IsMatchedBy(actual, timeSpan))
{
Execute.Assertion
.BecauseOf(because, becauseArgs)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions