/// <summary> /// Asserts that a <see cref="DateTimeOffset"/> occurs a specified amount of time before another <see cref="DateTimeOffset"/>. /// </summary> /// <param name="target"> /// The <see cref="DateTimeOffset"/> to compare the subject with. /// </param> /// <param name="because"> /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not /// start with the word <i>because</i>, it is prepended to the message. /// </param> /// <param name="reasonArgs"> /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders. /// </param> public AndConstraint <DateTimeOffsetAssertions> Before(DateTimeOffset target, string because = "", params object[] reasonArgs) { bool success = Execute.Assertion .ForCondition(subject.HasValue) .BecauseOf(because, reasonArgs) .FailWith("Expected date and/or time {0} to be " + predicate.DisplayText + " {1} before {2}{reason}, but found a <null> DateTime.", subject, timeSpan, target); if (success) { var actual = target.Subtract(subject.Value); if (!predicate.IsMatchedBy(actual, timeSpan)) { Execute.Assertion .BecauseOf(because, reasonArgs) .FailWith( "Expected date and/or time {0} to be " + predicate.DisplayText + " {1} before {2}{reason}, but it differs {3}.", subject, timeSpan, target, actual); } } return(new AndConstraint <DateTimeOffsetAssertions>(parentAssertions)); }
/// <summary> /// Asserts that a <see cref="DateTime"/> occurs a specified amount of time before another <see cref="DateTime"/>. /// </summary> /// <param name="target"> /// The <see cref="DateTime"/> to compare the subject with. /// </param> /// <param name="because"> /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not /// start with the word <i>because</i>, it is prepended to the message. /// </param> /// <param name="becauseArgs"> /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders. /// </param> public AndConstraint <TAssertions> Before(DateTime target, string because = "", params object[] becauseArgs) { bool success = Execute.Assertion .ForCondition(subject.HasValue) .BecauseOf(because, becauseArgs) .FailWith("Expected date and/or time {0} to be " + predicate.DisplayText + " {1} before {2}{reason}, but found a <null> DateTime.", subject, timeSpan, target); if (success) { TimeSpan actual = target - subject.Value; Execute.Assertion .ForCondition(predicate.IsMatchedBy(actual, timeSpan)) .BecauseOf(because, becauseArgs) .FailWith( "Expected {context:the date and time} {0} to be " + predicate.DisplayText + " {1} before {2}{reason}, but it is " + PositionRelativeToTarget(subject.Value, target) + " by {3}.", subject, timeSpan, target, actual.Duration()); } return(new AndConstraint <TAssertions>(parentAssertions)); }