Shouldly
  • Overview
  • Contributing
  • Documentation
    • Getting Started
    • Configuration
    • Equality
      • ShouldBe
      • NotBe
      • Null
      • Bool
      • Flags
      • AssignableTo
      • OfType
      • OneOf
      • Greater/Less Than
      • InRange
      • MatchApproved
      • Enumerable
      • SameAs
      • String
      • ExampleClasses
    • String
      • ShouldBe
      • Match
      • Contain
      • Null and Empty
      • StartWith
      • EndWith
    • Enumerable
      • ShouldBe
      • All
      • Empty
      • OneOf
      • Contain
      • Unique
      • SubsetOf
      • Have
    • Dictionary
      • ContainKey
      • ContainKeyAndValue
    • Exceptions
      • Throw
      • NotThrow
    • SatisfyAllConditions
    • CompleteIn
    • DynamicShould
    • Upgrade 3 to 4
Powered by GitBook
On this page
  • ShouldBeNull
  • ShouldBeNull (nullable value type)
  • ShouldNotBeNull
  • ShouldNotBeNull (nullable value type)
  • ShouldNotBeNull with chaining
  • ShouldNotBeNull with chaining (nullable value type)
Edit on GitHub
  1. Documentation
  2. Equality

Null

PreviousNotBeNextBool

Last updated 2 years ago

ShouldBeNull and ShouldNotBeNull allow you to check whether a value is null.

ShouldNotBeNull returns the non-null value if it succeeds so that further assertions can be chained. When used with a reference type, the returned value is the same reference annotated as non-null. Equivalently, when used on a System.Nullable<T> expression, the returned value is the unwrapped T value.

ShouldBeNull

var myRef = "Hello World";
myRef.ShouldBeNull();

|

Exception

myRef
    should be null but was
"Hello World"

ShouldBeNull (nullable value type)

int? nullableValue = 42;
nullableValue.ShouldBeNull();

|

Exception

nullableValue
    should be null but was
42

ShouldNotBeNull

string? myRef = null;
myRef.ShouldNotBeNull();

Exception

myRef
    should not be null but was

ShouldNotBeNull (nullable value type)

int? myRef = null;
myRef.ShouldNotBeNull();

Exception

myRef
    should not be null but was

ShouldNotBeNull with chaining

var myRef = (string?)"1234";
myRef.ShouldNotBeNull().Length.ShouldBe(5);

Exception

myRef.ShouldNotBeNull().Length
    should be
5
    but was
4

ShouldNotBeNull with chaining (nullable value type)

SomeStruct? nullableValue = new SomeStruct { IntProperty = 41 };
nullableValue.ShouldNotBeNull().IntProperty.ShouldBe(42);

Exception

nullableValue.ShouldNotBeNull().IntProperty
    should be
42
    but was
41

|

|

|

|

snippet source
snippet source
anchor
anchor
snippet source
anchor
snippet source
anchor
snippet source
anchor
snippet source
anchor