Question

I am trying to use the Fluent Assertions ShouldBeNull() extension method to assert that my instance is indeed null.

Result.ShouldBeNull();

Clearly, this throws an exception because I cannot call the extension on a null instance.

System.NullReferenceException

How do I use Fluent Assertions properly to make this assertion?

Was it helpful?

Solution

Extension methods can be called on objects that are null.

See for example http://bradwilson.typepad.com/blog/2008/01/c-30-extension.html

Think of extension methods not as instance methods, but static methods that take the object (Result in your case) as a first parameter.

OTHER TIPS

I wonder if you actually used FluentAssertions, because then your call should have looked like:

Result.Should().BeNull()

So with the dot .

Result needs to be nullable

result?.Should().BeNull();

Even if you're checking for null, you're still trying to do an operation on a null value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top