使用FluentAssertions比较可空类型与其基础类型时,这是一个错误吗?

Jul*_*ian 6 c# comparison nunit nullable fluent-assertions

我正在为一个实用程序库编写一些单元测试,当我遇到一个我希望实际上通过的测试失败时.该问题与比较两个float变量,而不是比较float?一个float变量和一个变量有关.

我正在使用NUnit(2.6.0.12051)和FluentAssertions(1.7.1)的最新版本,下面是一个小代码剪辑,说明了这个问题:

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.)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你可以从我的意见看,TestFloatEquality()一个正确失败(注释掉第一个失败的测试获得的第二个).

TestNullableFloatEquality()然而,d穿过但Ç失败.我原本以为C也会失败.只是提到它,如果我使用NUnit添加断言:

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

那些通过和失败的预期.

所以,对于这个问题:这是FluentAssertions中的错误,还是我错过了可空的比较?

Den*_*men 4

这是一个错误。我在 1.7.x 版本分支和主干中修复了这个问题。新 2.0.0 的开发仍在进行中,因此我们最终可能会决定发布 1.7.2 版本。

有关确切状态,请参阅http://fluidassertions.codeplex.com/workitem/12199 。