1
Vote

Fluent assertions v1.7.1 fails to compare nullable timespans

description

Not tested with latest version. It seems that an overload on SimpleTimeSpanAssertions which accepts a Nullable<TimeSpan> is missing. I was able to work around this by writing the next extension method:
public static class SimpleTimeSpanAssertionsExtensions
{
    // The current version of Fluent Assertions (v1.7.1) seems unable to compare nullable timespans.
    // Example: myObject.OptionalTime.Should().Be(expectedOptionalTime);
    // This method implements a workaround for this problem.
    public static void Be(this SimpleTimeSpanAssertions source, TimeSpan? expected)
    {
        if (expected == null)
        {
            source.Subject.As<object>().Should().BeNull();
        }
        else
        {
            source.Subject.Should().Be(expected.Value);
        }
    }
}
I am aware that the return type should not be void and that this method does not have parameters for describing the error. But for the moment it fixes my problem at hand.

Best regards,
Bart Koelman

comments

BartKoelman wrote Sep 28, 2012 at 10:33 PM

To clarify, without my extension method a compile-time error occurs:

The best overloaded method match for 'FluentAssertions.Assertions.SimpleTimeSpanAssertions.Be(System.TimeSpan)' has some invalid arguments

Argument 1: cannot convert from 'System.TimeSpan?' to 'System.TimeSpan'

dennisdoomen wrote Sep 29, 2012 at 7:19 AM

Have you tried this with the 2.0 beta? We're about to finalize the beta phase (as soon as MS releases an WP SDK for VS2012), so we won't issue any bug fixes anymore for 1.7.1

BartKoelman wrote Sep 29, 2012 at 8:13 AM

Tried with the 2.0 Beta, .NET 4.5 build. Same problem.

Example to reproduce:
public class BugCase
{
    public TimeSpan? OptionalTime { get; set; };

    public void Reproduce()
    {
        TimeSpan? expected = 5.Minutes();

        OptionalTime.Should().Be(expected); // compile-time error, see below
    }
The best overloaded method match for 'FluentAssertions.Primitives.SimpleTimeSpanAssertions.Be(System.TimeSpan, string, params object[])' has some invalid arguments