導入
Assert.Equal(expected, actual)より、文章のように読める検証を書けるNuGetライブラリがFluentAssertionsです。actual.Should().Be(expected)という語順で、失敗メッセージも分かりやすくなります。
図解
flowchart LR
A["Assert.Equal(5, x)"] -->|書き換え| B["x.Should().Be(5)"]
B --> R["主語→検証と読める<br/>失敗メッセージも文章的"]
style B fill:#e8f5e9
サンプル
// --- FluentAssertions の記法(読み物) ---
// order.Total.Should().Be(3200);
// order.Total.Should().BeGreaterThan(0);
// order.Items.Should().HaveCount(2);
// order.Items.Should().Contain("本");
// 対応表: Assert.Equal(3200, x) → x.Should().Be(3200)
// Assert.True(x > 0) → x.Should().BeGreaterThan(0)
// Assert.Contains("本", list) → list.Should().Contain("本")
// ブラウザでは「文章的な検証ヘルパー」を自作して考え方を体感する
int total = 3200;
Check(total).Be(3200).BeGreaterThan(0);
Console.WriteLine("検証成功");
Checker Check(int value) => new Checker(value);
class Checker
{
private readonly int _value;
public Checker(int value) => _value = value;
public Checker Be(int expected)
{
if (_value != expected) throw new Exception($"期待 {expected} だが {_value}");
return this; // メソッドチェーンで続けて検証できる
}
public Checker BeGreaterThan(int min)
{
if (_value <= min) throw new Exception($"{_value} は {min} より大きくない");
return this;
}
}
actual.Should().Be(expected)のように、主語→検証の語順で読めるBeGreaterThan/HaveCount/Containなど豊富な検証メソッドがある- 失敗メッセージが文章的で、CIが赤くなったとき原因が掴みやすい
演習
Check("hello").HaveLength(5).StartWith("he");
Console.WriteLine("検証成功");
// TODO: 文字列用の検証ヘルパー Checker を完成させてください。
// HaveLength(int)(長さ一致)と StartWith(string)(先頭一致)を実装し、
// いずれも this を返してチェーンできるようにします
Checker Check(string value) => new Checker(value);
___
- 期待される出力:
検証成功
ヒント1を見る
class Checker { private readonly string _v; public Checker(string v) => _v = v; public Checker HaveLength(int n) { if (_v.Length != n) throw new Exception(); return this; } public Checker StartWith(string p) { if (!_v.StartsWith(p)) throw new Exception(); return this; } }
ヒント2を見る
各メソッドの最後でreturn this;するとチェーンできます
まとめ
- FluentAssertionsは
x.Should().Be(y)で読みやすい検証を書く - 豊富な検証メソッドと分かりやすい失敗メッセージが利点
- Moqと合わせて実務テストの二大NuGetライブラリ
次章: 待ち時間を有効に使う「非同期プログラミング」へ進みます。