Skip to main content

Object

Describes the possible expectations for any object.

ExpectationNegatedSummary
IsEqualToIsNotEqualToequal to the expected object
IsSameAsIsNotSameAsthe same reference as the expected object
IsEquatableToIsNotEquatableToequal according to its IEquatable<T> implementation
IsEquivalentToIsNotEquivalentTostructurally equal to the expected object
IsOneOfIsNotOneOfequal to one of the expected objects
Is<T>IsNot<T>of type T or a derived type
IsExactly<T>IsNotExactly<T>exactly of type T
IsNullIsNotNullnull
SatisfiesDoesNotSatisfysatisfies a predicate
CompliesWithDoesNotComplyWithmeets a nested expectation
Whosea member meets a nested expectation

Equality​

You can verify that the object is equal to another one or not:

record Album(string Title);
Album album = new("Abbey Road");

await Expect.That(album).IsEqualTo(new Album("Abbey Road"));
await Expect.That(album).IsNotEqualTo(new Album("Revolver"));

This uses the object.Equals(object?, object?) method.

note

A number typed as object also equals a number of another numeric type with the same value (e.g. 1 and 1L) in equality (IsEqualTo, IsOneOf, Contains, ContainsValue), but not in equivalency.

Reference equality​

You can verify that the object has the same reference as another one:

record Album(string Title);
Album album = new("Abbey Road");

await Expect.That(album).IsSameAs(album);
await Expect.That(album).IsNotSameAs(new Album("Abbey Road"));

This uses the object.ReferenceEquals(object?, object?) method.

Custom comparer​

You can verify that the object is equal to another one while using a custom IEqualityComparer<object>:

class AlbumComparer : IEqualityComparer<object>
{
public bool Equals(object? x, object? y)
=> (x as Album)?.Title == (y as Album)?.Title;
public int GetHashCode(object obj)
=> obj.GetHashCode();
}
Album album = new("Abbey Road");

await Expect.That(album).IsEqualTo(new Album("Abbey Road")).Using(new AlbumComparer());

IEquatable<T>​

You can verify that the object is equal to a value by using its IEquatable<T> implementation, also when the value has a different type:

class TrackId(long value) : IEquatable<long>
{
public bool Equals(long other) => value == other;
}
TrackId trackId = new(42);

await Expect.That(trackId).IsEquatableTo(42L);
await Expect.That(trackId).IsNotEquatableTo(7L);
note

This inspects the subject by calling its IEquatable<T>.Equals(T) method. Therefore, IsEquatableTo and IsNotEquatableTo fail for a null subject, even IsEquatableTo(null), whereas IsEqualTo(null) succeeds.

Equivalency​

You can verify that the object is structurally equivalent to another one. See the equivalency page for details and configuration options:

Album album = new("Abbey Road");

await Expect.That(album).IsEquivalentTo(new Album("Abbey Road"));
await Expect.That(album).IsNotEquivalentTo(new Album("Revolver"));

One of​

You can verify that the object is one of many alternatives:

record Album(string Title);
Album album = new("Abbey Road");

await Expect.That(album).IsOneOf([new Album("Abbey Road"), new Album("Revolver")]);
await Expect.That(album).IsNotOneOf([new Album("Revolver"), new Album("Help!")]);

Type check​

You can verify that the object is of a given type or a derived type, or that it is not:

object album = new Album("Abbey Road");

await Expect.That(album).Is<Album>();
await Expect.That(album).Is(typeof(Album));
await Expect.That(album).IsNot<Track>();
await Expect.That(album).IsNot(typeof(Track));

IsExactly and IsNotExactly do not accept a derived type:

object album = new Album("Abbey Road");

await Expect.That(album).IsExactly<Album>();
await Expect.That(album).IsExactly(typeof(Album));
await Expect.That(album).IsNotExactly<Track>();
await Expect.That(album).IsNotExactly(typeof(Track));

Null​

You can verify if the object is null or not:

object? album = null;

await Expect.That(album).IsNull();
await Expect.That(new Album("Abbey Road")).IsNotNull();

Condition​

You can verify that any object satisfies a given predicate:

Track track = new() { Title = "Let It Be", PlayCount = 3 };

await Expect.That(track).Satisfies(x => x.PlayCount > 0);
await Expect.That(track).DoesNotSatisfy(x => x.IsPlayed);

When the object changes in the background, Within(…) waits until it satisfies the predicate, see waiting for a condition.

Nested expectation​

You can verify that any object complies with an expectation:

List<Track> tracks = new();

await Expect.That(tracks).CompliesWith(x => x.IsEmpty());
await Expect.That(tracks).DoesNotComplyWith(x => x.HasCount().GreaterThan(0));

DoesNotComplyWith is the exact inverse of CompliesWith: it succeeds as soon as the nested expectation fails. Like Satisfies, CompliesWith can wait for the object with Within(…).