【问题标题】:Performing assertions on multiple properties of a Class对类的多个属性执行断言
【发布时间】:2016-05-12 18:31:55
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: fluent-assertions


    【解决方案1】:

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

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

    【讨论】:

    • 不错的一个。我越来越喜欢这个了。
    • 虽然很好,但我看到这个解决方案的问题是它不是强类型的。
    • 你可以用result.Should().BeOfType&lt;MyClass&gt;().And.ShouldBeEquivalentTo(....链接它
    【解决方案2】:

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

    这就是我最终要做的:

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

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 2016-06-21
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多