CodeMain:Visual Studio代碼自動整理插件!
地址: https://mp.weixin.qq.com/s/mtOApIRqFzOReVAhF2k1Bw
FluentAssertions:C#單元測試斷言庫,讓測試代碼更加直觀、易讀!
地址:https://github.com/fluentassertions/fluentassertions
FluentAssertions 是一個基於 .NET 的斷言庫,以幫助開發者更高效地管理和維護代碼。 它提供了一種鏈式調用的方式來編寫斷言語句。支持框架.NET Framework 4.7、.NET Core 2.1、.NET Core 3.0、.NET 6、.NET Standard 2.0/2.1,支持單元測試框架MSTest2、NUnit3、XUnit2、MSpec和NSpec3。
1、更接近於自然語言
傳統寫法:
Assert.AreEqual(5, result);
使用 FluentAssertions:
result.Should().Be(5);
2、多個斷言條件,一個語句中連續進行多個斷言
string actual = "ABCDEFGHI";
actual.Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);
3、除了基本的等值斷言外,FluentAssertions 還提供了豐富的斷言方法,用於驗證各種不同類型的數據和場景。例如,你可以使用 BeGreaterThan 方法來驗證一個數值是否大於另一個數值:
int largerNumber = 10;
int smallerNumber = 5;
largerNumber.Should().BeGreaterThan(smallerNumber);