当我遇到一个我希望失败的测试时,我正在为实用程序库编写一些单元测试。该问题与比较两个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.)
}
}
}
从我的评论中可以看出,在TestFloatEquality()
中, A 和 B 都正确失败(只是注释掉第一个失败的测试才能到达第二个一个)。
然而,在TestNullableFloatEquality()
中, D 通过但 C 失败。我本来希望 C 在这里失败。只是提到它,如果我使用NUnit添加断言:
Assert.AreEqual(expected, actual); // Passes
Assert.AreEqual(notExpected, actual); // Fails (Expected: 1.0f But was: 3.1400001f)
那些通过和失败的预期。
所以,问题是:这是FluentAssertions中的错误,还是我错过了可空的比较?
答案 0 :(得分:4)
这是一个错误。我在1.7.x发布分支和trunk中修复了这个问题。新2.0.0的开发仍在进行中,因此我们可能会决定在1.7.2版本之后发布。
请参阅http://fluentassertions.codeplex.com/workitem/12199了解确切状态。