【问题标题】:FluentAssertions Type checkFluentAssertions 类型检查
【发布时间】:2016-01-28 01:42:09
【问题描述】:

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

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

很遗憾,我的测试失败并显示以下错误消息:

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

为什么说它找到了System.RuntimeType? 我用调试器验证了MyProperty1TypeB类型,它是……我用.BeOfType&lt;&gt;错了吗?

【问题讨论】:

  • 你能跳过 .GetType() 吗?您问的不是 MyProperty1 的类型,而是类型的类型。它太深了 1 级。

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


【解决方案1】:

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

【讨论】:

  • 天哪,我的错……异常消息甚至非常明显……谢谢:-)
猜你喜欢
  • 2014-08-01
  • 2021-09-02
  • 2018-10-15
  • 1970-01-01
  • 2021-11-09
  • 2020-04-12
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
相关资源
最近更新 更多