NotBe

ShouldNotBe is the inverse of ShouldBe.

Objects

ShouldNotBe works on all types and compares using .Equals.

var theSimpsonsCat = new Cat { Name = "Santas little helper" };
theSimpsonsCat.Name.ShouldNotBe("Santas little helper");

snippet source | anchor

Exception

theSimpsonsCat.Name
    should not be
"Santas little helper"
    but was

Numeric

ShouldNotBe also allows you to compare numeric values, regardless of their value type.

Integer

const int one = 1;
one.ShouldNotBe(1);

snippet source | anchor

Exception

one
    should not be
1
    but was

Long

const long aLong = 1L;
aLong.ShouldNotBe(1);

snippet source | anchor

Exception

aLong
    should not be
1L
    but was

DateTime(Offset)

ShouldNotBe DateTime overloads are similar to the numeric overloads and also support tolerances.

var date = new DateTime(2000, 6, 1);
date.ShouldNotBe(new(2000, 6, 1, 1, 0, 1), TimeSpan.FromHours(1.5));

snippet source | anchor

Exception

date
    should not be within
01:30:00
    of
2000-06-01T01:00:01.0000000
    but was
2000-06-01T00:00:00.0000000

TimeSpan

TimeSpan also has tolerance overloads

var timeSpan = TimeSpan.FromHours(1);
timeSpan.ShouldNotBe(timeSpan.Add(TimeSpan.FromHours(1.1d)), TimeSpan.FromHours(1.5d));

snippet source | anchor

Exception

timeSpan
    should not be within
01:30:00
    of
02:06:00
    but was
01:00:00

Last updated