FluentAssertions类型检查

xer*_*him 8 .net c# nunit unit-testing fluent-assertions

我尝试使用FluentAssertions来检查我的UnitTest,项目列表中属性的类型是某种类型.

myObj.Items.OfType<TypeA>().Single()
            .MyProperty1.GetType()
                .Should().BeOfType<TypeB>();
Run Code Online (Sandbox Code Playgroud)

不幸的是,我的测试失败,出现以下错误消息:

预期类型为TypeB,但找到System.RuntimeType.

它为什么说,它找到了System.RuntimeType?我使用调试器来验证,这MyProperty1是类型TypeB,它是......我使用.BeOfType<>错了吗?

提前致谢

nto*_*ohl 14

请跳过.GetType().你问的不是MyProperty1的类型,而是类型的类型.这是1级太深了.

public class TypeB { }

public class TypeA
{
    public TypeB MyProperty1 { get; set; }

    public TypeA()
    {
        MyProperty1 = new TypeB();
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        List<object> objects = new List<object>();
        objects.Add("alma");
        objects.Add(new TypeA());
        objects.OfType<TypeA>().Single().MyProperty1.Should().BeOfType<TypeB>();
    }
}
Run Code Online (Sandbox Code Playgroud)