Enum
Describes the possible expectations for enum values.
| Expectation | Negated | Summary |
|---|---|---|
IsEqualTo | IsNotEqualTo | equal to the expected value |
IsOneOf | IsNotOneOf | equal to one of the expected values |
HasValue | negated comparison | has the expected underlying numeric value |
IsDefined | IsNotDefined | a named member of the enum |
HasFlag | DoesNotHaveFlag | has the expected flag set |
The samples on this page use the following enum:
enum Genre { Rock = 1, Pop = 2, Jazz = 3, Blues = 4 }
Equality
You can verify that the enum is equal to another one or not:
await Expect.That(Genre.Rock).IsEqualTo(Genre.Rock);
await Expect.That(Genre.Rock).IsNotEqualTo(Genre.Jazz);
One of
You can verify that the enum is one of many alternatives:
await Expect.That(Genre.Rock).IsOneOf(Genre.Rock, Genre.Pop, Genre.Jazz);
await Expect.That(Genre.Blues).IsNotOneOf(Genre.Rock, Genre.Pop, Genre.Jazz);
Value
You can verify that the enum has a given underlying numeric value or not:
await Expect.That(Genre.Rock).HasValue(1);
await Expect.That(Genre.Rock).HasValue().NotEqualTo(2);
await Expect.That(Genre.Jazz).HasValue().GreaterThan(2);
The Has… expectations for a number or a TimeSpan, e.g. HasLength(), HasCount(), HasYear() or HasMajor(),
continue with one of the following comparisons, each with a negated counterpart:
| Comparison | Negated | Succeeds when the value is |
|---|---|---|
EqualTo(x) | NotEqualTo(x) | equal to x |
GreaterThan(x) | NotGreaterThan(x) | greater than x |
GreaterThanOrEqualTo(x) | NotGreaterThanOrEqualTo(x) | greater than or equal to x |
LessThan(x) | NotLessThan(x) | less than x |
LessThanOrEqualTo(x) | NotLessThanOrEqualTo(x) | less than or equal to x |
Between(min).And(max) | NotBetween(min).And(max) | between min and max, both bounds included |
Passing the value directly, e.g. HasLength(10), is a shorthand for EqualTo.
The expected value is nullable, e.g. to pass a value mapped from a property. As the actual value is never null,
EqualTo(null) fails and NotEqualTo(null) succeeds, while a comparison of order against null, such as
GreaterThan(null) or Between(null).And(3), fails even when negated.
Every backing type from sbyte to ulong is covered. Each comparison takes a long or a ulong, so a member of
a ulong-backed enum above long.MaxValue can be named as well:
enum Big : ulong { Max = ulong.MaxValue }
await Expect.That(Big.Max).HasValue(ulong.MaxValue);
await Expect.That(Big.Max).HasValue().GreaterThan(0);
Defined
You can verify that the enum has a defined value or not:
await Expect.That((Genre)3).IsDefined()
.Because("3 corresponds to 'Jazz'");
await Expect.That((Genre)5).IsNotDefined()
.Because("5 is no valid genre");
Flags
You can verify that the enum has a specific flag or not:
using System.Text.RegularExpressions;
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
await Expect.That(options).HasFlag(RegexOptions.IgnoreCase);
await Expect.That(options).DoesNotHaveFlag(RegexOptions.ExplicitCapture);
Unlike the property-style Has… expectations such as HasValue, HasFlag has no continuation: it asks whether a bit
is set, not how two ordered values compare, so GreaterThan, Between and the rest would have no meaning for it.