fluentassertions: x.Should().NotBeNull().And.Subject returns object rather than the type of x

It turns out that x.Should().NotBeNull().And returns ObjectAssertions having its Subject of type object rather than of x. So you can’t nest assertions.

Foo foo = new Foo();
AndConstraint<ObjectAssertions> c = foo.Should().NotBeNull();
ObjectAssertions a = c.And;
object s = a.Subject; // here I'd expect Foo

How can it be enhanced?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (18 by maintainers)

Most upvoted comments

I wish I could use this API in strongly-typed manner so could access subject’s properties, e.g.:

foo.Should().NotBeNull()
      .And.Subject.Bar.NotBeNull();

In case you want to prevent “possible NullReferenceException” warnings, this may help.

Example usage:

// Assert
Customer firstCustomer = customers[0].ShouldNotBeNull();
firstCustomer.Name.Should().Be("John Doe");