#Fluent assertions with NExpect
Quick Reference
Why
The why of NExpect is covered in detail in a blog post. Basically, after working with Javascript assertion frameworks (Chai and Jasmine), I wasn’t satisfied with NUnit assertions. I wanted something:
- more expressive
- easier to extend
- with similar syntax, since I was switching back and forth between Javascript and C# environments
The result is NExpect, which I’m quite happy with, and which has proven (to me at least) that there are some benefits in tacking an assertions framework from a user-first, expression-first perspective.
How to get it
NExpect should be available via Nuget. In addition, there are some add-on packages which provide out-of-the-box matchers for specialised environments:
- NExpect.Matchers.NSubstitute
provides
Expect
syntax for NSubstitute expressions - NExpect.Matchers.AspNetCore provides easier assertions against routing and verbs for asp.net core controllers
How to use it
- install, via Nuget
- statically import
Expectations
:using static NExpect.Expectations;
- depending on usage, you may also need:
using NExpect;
- start expecting!
[Test] public void SomeTest() { // Arrange var sut = new SystemUnderTest(); // Act var result = sut.PerformSomeAction(); // Assert Expect(result).To.Be.True(); }
There are a few introductory blog posts about usage: