stonedauwg
stonedauwg

Reputation: 1407

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

Upvotes: 4

Views: 1860

Answers (1)

Dennis Doomen
Dennis Doomen

Reputation: 8889

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");

Upvotes: 5

Related Questions