ユーティリティ ライブラリの単体テストをいくつか書いていたときに、失敗すると予想されていたテストが実際には合格したことに気づきました。この問題は、1 つの変数と 1 つの変数を比較するのではなく、2 つの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.)
}
}
}
私のコメントからわかるように、AとBTestFloatEquality()
の両方で正しく失敗します (最初に失敗したテストをコメントアウトして、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許容比較に関して何か不足していますか?