Skip to content

Objects

ObjectAssertions<T> is the base assertion class, available for any type via .Should().

Null Checks

string? value = null;

value.Should().BeNull();

string nonNull = "hello";
nonNull.Should().NotBeNull();

NotBeNull() returns an ObjectAssertionsChain<T> so you can continue asserting:

string? maybeNull = GetValue();
maybeNull.Should().NotBeNull().And.Equal("expected");

Equality

Assert equality using the default EqualityComparer<T>:

"hello".Should().Equal("hello");
"hello".Should().NotEqual("world");

Pass a custom IEqualityComparer<T>:

"Hello".Should().Equal("hello", StringComparer.OrdinalIgnoreCase);

Or a predicate:

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:

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):

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:

object value = 42;
value.Should().NotBeOfType<string>();

See BeOfType and NotBeOfType.