Is this a bug when comparing a nullable type with its underlying type using FluentAssertions?
Asked Answered
M

1

6

I was writing some unit tests for a utility library when I came across a test I would expect to fail that actually passed. The issue is related to comparing two float variables, versus comparing one float? and one float variable.

I'm using the latest versions of both NUnit (2.6.0.12051) and FluentAssertions (1.7.1), and below is a small code snipped illustrating the issue:

using FluentAssertions;
using FluentAssertions.Assertions;
using NUnit.Framework;

namespace CommonUtilities.UnitTests
{
    [TestFixture]
    public class FluentAssertionsFloatAssertionTest
    {
        [Test]
        public void TestFloatEquality()
        {
            float expected = 3.14f;
            float notExpected = 1.0f;
            float actual = 3.14f;

            actual.Should().BeApproximately(expected, 0.1f);
            actual.Should().BeApproximately(notExpected, 0.1f); // A: Correctly fails (Expected value 3,14 to approximate 1 +/- 0,1, but it differed by 2,14.)
            actual.Should().BeInRange(expected, expected);
            actual.Should().BeInRange(notExpected, notExpected); // B: Correctly fails (Expected value 3,14 to be between 1 and 1, but it was not.)
        }

        [Test]
        public void TestNullableFloatEquality()
        {
            float expected = 3.14f;
            float notExpected = 1.0f;
            float? actual = 3.14f;

            actual.Should().BeApproximately(expected, 0.1f);
            actual.Should().BeApproximately(notExpected, 0.1f); // C: Passes (I expected it to fail!)
            actual.Should().BeInRange(expected, expected);
            actual.Should().BeInRange(notExpected, notExpected); // D: Correctly fails (Expected value 3,14 to be between 1 and 1, but it was not.)
        }
    }
}

As you can see from my comments, in TestFloatEquality() both A and B fails correctly (just comment out the first failing test to get to the second one).

In TestNullableFloatEquality() however, D passes but C fails. I would have expected C to fail here as well. And just to have mentioned it, if I add assertions using NUnit:

Assert.AreEqual(expected, actual); // Passes
Assert.AreEqual(notExpected, actual); // Fails (Expected: 1.0f But was:  3.1400001f)

those pass and fail as expected.

So, to the question: Is this a bug in FluentAssertions, or am I missing something with respect to nullable comparison?

Mendiola answered 25/5, 2012 at 9:57 Comment(0)
H
4

It was a bug. I fixed this in both the 1.7.x release branch as well as in the trunk. Development on the new 2.0.0 is still ongoing, so we might decide to do a 1.7.2 release after all.

See http://fluentassertions.codeplex.com/workitem/12199 for the exact status.

Harrietteharrigan answered 29/5, 2012 at 11:23 Comment(1)
It's in the 2.0 beta version at fluentassertions.codeplex.com/releases/view/82423Harrietteharrigan

© 2022 - 2024 — McMap. All rights reserved.