[英]How can I make FluentAssertions ShouldBeEquivalentTo check for type when comparing?
[英]FluentAssertions Type check
我尝试使用FluentAssertions来检查我的 UnitTest,项目列表中的属性类型是否属于某种类型。
myObj.Items.OfType<TypeA>().Single()
.MyProperty1.GetType()
.Should().BeOfType<TypeB>();
不幸的是,我的测试失败并显示以下错误消息:
预期类型为 TypeB,但发现 System.RuntimeType。
为什么说它找到了System.RuntimeType
? 我使用调试器来验证MyProperty1
是TypeB
类型,它是...我使用.BeOfType<>
错了吗?
请跳过 .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>();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.