我从文档中知道我可以做到这一点......
result.Should().BeOfType<MyClass>().Which.Property1.Should().Be("String")
有没有办法以类似的方式测试多个属性
result.Should().BeOfType<MyClass>().Which.Property1.Should().Be("String").And.Property2.Should().Be(99);
如果可以执行上述任一测试而不必断言它们是"OfType",那也很好,但我怀疑代码没有其他方法可以知道哪些属性可用。
您可以对匿名类型执行结构比较断言,如下所示:
result.ShouldBeEquivalentTo(new
{
Property1 = "String",
Property2 = 99
}, options => options.ExcludingMissingMembers());
一个简单的解决方案是使用 BeOfType<>() 返回的 AndWhichConstraint 类型。
这就是我最终要做的:
var myClassType = result.Should().BeOfType<MyClass>;
myClassType.Which.Property1.Should().Be("String");
myClassType.Which.Property2.Should().Be(99);