2

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

AVANISH RAJBHAR
  • 527
  • 3
  • 9
stonedauwg
  • 1,328
  • 1
  • 14
  • 35
  • `Which` can be use on collection or single object as well https://fluentassertions.com/introduction – Nkosi Feb 25 '20 at 14:33
  • @Nkosi That page only states "...ability to chain a specific assertion on top of an assertion that acts on a collection or graph of objects", hence my question. I think you are likely right, but some official documentation confirming that or a diff way than using 'Which' would be nice – stonedauwg Feb 25 '20 at 15:00

1 Answers1

4

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");
Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • yes thats true and what I stated in my question. Was looking for *why* have to use 'Which' instead of 'And' and why their docs state 'Which' is for collections and object graphs – stonedauwg Feb 26 '20 at 16:12
  • `And` just returns the assertion class that you started with and on which `BeAssignableTo` is defined. `Which` is available when that particular assertion makes sense and there's some typed object to continue on. – Dennis Doomen Feb 26 '20 at 20:18