How to use Fluid Assertions .ShouldBeNull()

1.3k views Asked by At

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?

3

There are 3 answers

0
Cristian Lupascu On BEST ANSWER

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.

0
Dennis Doomen On

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

Result.Should().BeNull()

So with the dot .

0
Stourme On

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.