What Is It?
Should Assertion Library (“Should” for short) is a library of test assertion methods for .NET. It is totally independent of, and completely compatible with any testing framework. It was created by Eric Hexter, and I subsequently added a fluent interface which I ported from ShouldIt. ShouldIt is now deprecated.
Should comes in two flavors:
- normal (Should.dll)
- fluent (Should.Fluent.dll).
By the way, if you haven’t tried NuPack yet, go get it right now! You’ve got it? Okay, open the Package Management Console and enter:
> Install-Package Should
> Install-Package ShouldFluent
This will install Should as a package in the current solution. You probably want to use either the fluent API or the normal API in any given test project – but not both. Choosing one or the other will promote a consistent practice within your team. Obviously, I prefer the fluent API.
There’s not much to say about usage. It’s really pretty intuitive and discoverable. Just start typing “.Should” after your actual object, and you will see what’s available. To give you a taste, here’s a simple example:
actual.ShouldNotBeNull(); actual.ShouldBeType<Foo>(); actual.ShouldBeSameAs(expected);
actual .Should().Not.Be.Null() .Should().Be.OfType<Foo>() .Should().Be.SameAs(expected);
There are several examples of both the normal and the fluent syntax here.
A Note To ShouldIt Users: If you have been using ShouldIt, just remove the ShouldIt DLLs, add Should.Fluent.dll, and change the namespaces. Everything should just work. Of course depending on which test framework you were using with ShouldIt, the underlying assertion behavior could change slightly. It should be pretty easy to spot the differences and make any small corrections as needed.