ShouldNotBe
is the inverse of ShouldBe
.
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.Nameshould not be"Santas little helper"but was
ShouldNotBe
also allows you to compare numeric values, regardless of their value type.
const int one = 1;one.ShouldNotBe(1);
​snippet source | anchor
Exception
oneshould not be1but was
const long aLong = 1L;aLong.ShouldNotBe(1);
​snippet source | anchor
Exception
aLongshould not be1Lbut was
ShouldNotBe
DateTime overloads are similar to the numeric overloads and also support tolerances.
var date = new DateTime(2000, 6, 1);date.ShouldNotBe(new DateTime(2000, 6, 1, 1, 0, 1), TimeSpan.FromHours(1.5));
​snippet source | anchor
Exception
dateshould not be within01:30:00of2000-06-01T01:00:01.0000000but was2000-06-01T00:00:00.0000000
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
timeSpanshould not be within01:30:00of02:06:00but was01:00:00