Shouldly
Search…
Null
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

1
var myRef = "Hello World";
2
myRef.ShouldBeNull();
Copied!
Exception
1
myRef
2
should be null but was
3
"Hello World"
Copied!

ShouldBeNull (nullable value type)

1
int? nullableValue = 42;
2
nullableValue.ShouldBeNull();
Copied!
Exception
1
nullableValue
2
should be null but was
3
42
Copied!

ShouldNotBeNull

1
string? myRef = null;
2
myRef.ShouldNotBeNull();
Copied!
Exception
1
myRef
2
should not be null but was
Copied!

ShouldNotBeNull (nullable value type)

1
int? myRef = null;
2
myRef.ShouldNotBeNull();
Copied!
Exception
1
myRef
2
should not be null but was
Copied!

ShouldNotBeNull with chaining

1
var myRef = (string?)"1234";
2
myRef.ShouldNotBeNull().Length.ShouldBe(5);
Copied!
Exception
1
myRef.ShouldNotBeNull().Length
2
should be
3
5
4
but was
5
4
Copied!

ShouldNotBeNull with chaining (nullable value type)

1
SomeStruct? nullableValue = new SomeStruct { IntProperty = 41 };
2
nullableValue.ShouldNotBeNull().IntProperty.ShouldBe(42);
Copied!
Exception
1
nullableValue.ShouldNotBeNull().IntProperty
2
should be
3
42
4
but was
5
41
Copied!
Last modified 1yr ago