Is this a mistake when comparing a nullable type with its base type using FluentAssertions?

I wrote some unit tests for the utility library when I came across a test that I would expect a crash that actually passed. Is there a problem with comparing two float variables compared to one float? variable float? and one variable float .

I use the latest versions of both NUnit (2.6.0.12051) and FluentAssertions (1.7.1), and below is an abbreviated code illustrating the problem:

 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 will fail (just comment out the first failed test to go to the second).

In TestNullableFloatEquality() however, D passes, but C fails. I would expect C to fail too. And just to mention this, 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 due to a comparison with a null value?

+6
source share
1 answer

It was a mistake. I fixed it both in the 1.7.x release branches and in the trunk. The development of the new 2.0.0 is still ongoing, so we can decide to release 1.7.2.

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

+4
source

Source: https://habr.com/ru/post/916639/


All Articles