Skip to content

Fluent Assertions nullable 可空类型的断言 ​

csharp
short? theShort = null;
theShort.Should().NotHaveValue(); // 没有值
theShort.Should().BeNull(); //是 null 跟上面是等价的
theShort.Should().Match(x => !x.HasValue || x > 0); // 没值,或者值大于0 (predicate是不是能成功)

Fluent Assertions boolean 布尔类型的断言 ​

csharp
var theBoolean = false;
theBoolean.Should().BeFalse(); //是false

var theBoolean2 = true;
theBoolean.Should().Be(theBoolean2); // 是不是跟另一个变量一样

theBoolean.Should().BeTrue(); //出错

Fluent Assertions Guid 布尔类型的断言 ​

Guid 我们是经常来拿做数据库主键的。 对Guid 进行断言比较简单的。 您可以断言它们与另一个 Guid 相等,或者您可以断言 Guid 为空。

csharp
[Fact]
public void Test_Example1()
{
    Guid theGuid = Guid.NewGuid();
    Guid sameGuid = theGuid;
    Guid otherGuid = Guid.NewGuid();

    theGuid.Should().Be(sameGuid); //是不是跟另一个变量一样
    theGuid.Should().NotBe(otherGuid); //不等于另一个变量
    theGuid.Should().NotBeEmpty(); //非空的。 Guid的空值是 00000000-0000-0000-0000-000000000000

    Guid.Empty.Should().BeEmpty(); //空值
}

Fluent Assertions Enums枚举类型的断言 ​

枚举的时候有两种情况,正常的情况,还有位标志的情况。

csharp
public class MyTestClass
{
    [Fact]
    public void Test_Example1()
    {
        var p = Permission.Order;

        p.Should().Be(Permission.Order); //等于Permission.Order
        //位标志的情况
        p.Should().HaveFlag(Permission.Order); //含有 Permission.Order
        p.Should().NotHaveFlag(Permission.Product); //不含有 Permission.Product
    }

}

[Flags]
public enum Permission
{
    Product = 1,
    Order = 1 << 1,
}

简单就是美