FluentAssertions类型检查

时间:2015-10-28 10:21:09

标签: c# .net unit-testing nunit fluent-assertions

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

myObj.Items.OfType<TypeA>().Single()
            .MyProperty1.GetType()
                .Should().BeOfType<TypeB>();

不幸的是,我的测试失败并显示以下错误消息:

  

预期的类型   类型B,   但找到了System.RuntimeType。

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

提前致谢

1 个答案:

答案 0 :(得分:12)

请跳过.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>();
    }
}
相关问题