Objects
ObjectAssertions<T> is available for any type via .Should(). Its assertions are defined on ObjectAssertions<TSelf, T>, the base class for all the other assertion classes, so they are available for every type.
Null Checks
csharp
string? value = null;
value.Should().BeNull();
string nonNull = "hello";
nonNull.Should().NotBeNull();NotBeNull() returns an AssertionsChain<TAssertions, T> so you can continue asserting:
csharp
string? maybeNull = GetValue();
maybeNull.Should().NotBeNull().And.Equal("expected");Equality
Assert equality using the default EqualityComparer<T>:
csharp
"hello".Should().Equal("hello");
"hello".Should().NotEqual("world");Pass a custom IEqualityComparer<T>:
csharp
"Hello".Should().Equal("hello", StringComparer.OrdinalIgnoreCase);Or a predicate:
csharp
myObj.Should().Equal(other, (a, b) => a.Id == b.Id);See Equal and NotEqual for full overload details.
Reference Equality
Check whether two variables point to the same object instance:
csharp
var a = new object();
var b = a;
var c = new object();
a.Should().BeTheSameInstanceAs(b);
a.Should().NotBeTheSameInstanceAs(c);Type Checks
Assert that a value is of a specific type (using IsAssignableTo, so subtypes pass):
csharp
object value = "hello";
value.Should().BeOfType<string>();
// Returns a chain typed to the asserted type
value.Should().BeOfType<string>().And.Equal("hello");Assert that a value is not of a type:
csharp
object value = 42;
value.Should().NotBeOfType<string>();See BeOfType and NotBeOfType.