Shouldly
Search…
NotBe
ShouldNotBe is the inverse of ShouldBe.

Objects

ShouldNotBe works on all types and compares using .Equals.
1
var theSimpsonsCat = new Cat { Name = "Santas little helper" };
2
theSimpsonsCat.Name.ShouldNotBe("Santas little helper");
Copied!
Exception
1
theSimpsonsCat.Name
2
should not be
3
"Santas little helper"
4
but was
Copied!

Numeric

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

Integer

1
const int one = 1;
2
one.ShouldNotBe(1);
Copied!
Exception
1
one
2
should not be
3
1
4
but was
Copied!

Long

1
const long aLong = 1L;
2
aLong.ShouldNotBe(1);
Copied!
Exception
1
aLong
2
should not be
3
1L
4
but was
Copied!

DateTime(Offset)

ShouldNotBe DateTime overloads are similar to the numeric overloads and also support tolerances.
1
var date = new DateTime(2000, 6, 1);
2
date.ShouldNotBe(new DateTime(2000, 6, 1, 1, 0, 1), TimeSpan.FromHours(1.5));
Copied!
Exception
1
date
2
should not be within
3
01:30:00
4
of
5
2000-06-01T01:00:01.0000000
6
but was
7
2000-06-01T00:00:00.0000000
Copied!

TimeSpan

TimeSpan also has tolerance overloads
1
var timeSpan = TimeSpan.FromHours(1);
2
timeSpan.ShouldNotBe(timeSpan.Add(TimeSpan.FromHours(1.1d)), TimeSpan.FromHours(1.5d));
Copied!
Exception
1
timeSpan
2
should not be within
3
01:30:00
4
of
5
02:06:00
6
but was
7
01:00:00
Copied!
Last modified 1yr ago