3

我从文档中知道我可以做到这一点......

result.Should().BeOfType<MyClass>().Which.Property1.Should().Be("String")

有没有一种方法可以以类似的方式测试多个属性

result.Should().BeOfType<MyClass>().Which.Property1.Should().Be("String").And.Property2.Should().Be(99);

如果可以在不必断言它们是“OfType”的情况下进行上述任一测试也是很好的,但我怀疑代码没有其他方法可以知道哪些属性可用。

4

2 回答 2

3

您可以对匿名类型进行结构比较断言,如下所示:

result.ShouldBeEquivalentTo(new { Property1 = "String", Property2 = 99 }, options => options.ExcludingMissingMembers());

于 2016-02-03T17:55:26.917 回答
2

一种简单的解决方案是使用 BeOfType<>() 返回的 AndWhichConstraint 类型。

这就是我最终要做的:

var myClassType = result.Should().BeOfType<MyClass>;
myClassType.Which.Property1.Should().Be("String");
myClassType.Which.Property2.Should().Be(99);
于 2020-10-08T14:10:49.463 回答