Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent assertions - how to properly chain after a type check

Why can't I do the following with FluentAssertions, using the 'And' property?

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. Instead, I have to use 'Which' in place of And, which I thought was used for collections, not single objects. The Which version does compile but the semantics aren't as clear

like image 239
stonedauwg Avatar asked Oct 28 '25 02:10

stonedauwg


1 Answers

Which will give you a reference to SomeObject, but cast as OtherObject. So your example will change to:

SomeObject.Should()
   .BeAssignableTo<OtherObject>()
   .Which.SomeStringProperty.Should().StartWith("whatever");
like image 171
Dennis Doomen Avatar answered Oct 30 '25 18:10

Dennis Doomen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!