FluentAssertions is an alternative assertion library for unit tests, to use instead of the methods in Assert class that Microsoft provides. It has much better support for exceptions and some other stuff that improves readability and makes it easier to produce tests.
The coding of Kentor.AuthServices was a perfect opportunity for me to do some real TDD (Test Driven Development) again. I have long thought that the
[ExpectedException] attribute that MsTest offers is not enough, so when Albin Sunnanbo suggested that I’d look at FluentAssertions I decided to try it.
Is the grass greener on the other side? Trying FluentAssertions instead of plain MsTest Assert class and it's WAY better.
— Anders Abel (@anders_abel) September 17, 2013
FluentAssertions offers a
ShouldThrow() extension method to the
Action delegate type. It asserts that invoking a particular action will throw an exception.
// Code from https://github.com/KentorIT/authservices/blob/master/ // Kentor.AuthServices.Tests/Saml2ResponseTests.cs Action a = () => Saml2Response.Read(response).GetClaims(); a.ShouldThrow<InvalidOperationException>() .WithMessage("The Saml2Response must be validated first.");
Compared to the
[ExpectedException] attribute this offers much better control.