私は、実際に合格しなかったテストに遭遇したときに、ユーティリティライブラリの単体テストを書いていました。この問題は、変数float
を2つ比較して、1つの変数float?
と1つの変数float
を比較することに関連しています。FluentAssertionsを使用してヌル可能型と基底型を比較すると、これはバグですか?
私は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()
にあります。とBは正しく失敗します(2番目のテストに最初に失敗したテストをコメントアウトします)。 TestNullableFloatEquality()
で
しかしながら、Dパスが、Cに障害が発生しました。私はCがここでも失敗すると予想していました。 NUnitを使用してアサーションを追加すると、それを言及するだけです。
Assert.AreEqual(expected, actual); // Passes
Assert.AreEqual(notExpected, actual); // Fails (Expected: 1.0f But was: 3.1400001f)
期待通りに合格します。
質問:これはFluentAssertionsのバグですか?null可能な比較に関して何か不足していますか?
http://fluentassertions.codeplex.com/releases/view/82423の2.0ベータ版です –