FluentAssertions 类型检查
FluentAssertions Type check
我尝试使用 FluentAssertions 检查我的单元测试,项目列表中 属性 的类型属于特定类型。
myObj.Items.OfType<TypeA>().Single()
.MyProperty1.GetType()
.Should().BeOfType<TypeB>();
不幸的是,我的测试失败并显示以下错误消息:
Expected type to be
TypeB,
but found 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>();
}
}
我尝试使用 FluentAssertions 检查我的单元测试,项目列表中 属性 的类型属于特定类型。
myObj.Items.OfType<TypeA>().Single()
.MyProperty1.GetType()
.Should().BeOfType<TypeB>();
不幸的是,我的测试失败并显示以下错误消息:
Expected type to be TypeB, but found 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>();
}
}