[英]Fluent assertions - how to properly chain after a type check
Why can't I do the following with FluentAssertions, using the 'And' property?为什么我不能使用“And”属性对 FluentAssertions 执行以下操作?
SomeObject.Should()
.BeAssignableTo<OtherObject>()
.And
.SomeStringProperty.Should().StartWith("whatever");
That will not compile because after the And it doesn't know that it's a SomeObject type.这不会编译,因为在之后它不知道它是 SomeObject 类型。 Instead, I have to use 'Which' in place of And, which I thought was used for collections, not single objects.相反,我必须使用“Which”代替 And,我认为它用于集合,而不是单个对象。 The Which version does compile but the semantics aren't as clear哪个版本可以编译但语义不清楚
Which
will give you a reference to SomeObject
, but cast as OtherObject
. Which
会给你一个参考SomeObject
,但塑像作为OtherObject
。 So your example will change to:因此,您的示例将更改为:
SomeObject.Should()
.BeAssignableTo<OtherObject>()
.Which.SomeStringProperty.Should().StartWith("whatever");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.