What Is Fluent Assertions and Should I Be Using It?

Releasing a project without bugs is an essential part of every project.  One of the quickest and easiest tools to help you achieve that goal are unit tests.  Regardless of how high, or low your test coverage is, you should be writing unit tests. One the biggest benefits of unit testing (which is also one of the most overlooked) is that it essentially creates a spec that you or anyone on your team can reference now, or in the future, that describes what your code/classes should be doing and what they shouldn't.  

Many developers just think of unit tests as a means to an end.  If my class has tests, I've ticked some sort of magical code quality box and my code can be deemed as 'high quality'.  What a lot of people fail to understand, is that well-written unit tests can be thought of as an accompanying project document that, as developers, we can actually benefit from.  In a year's time, if a bug appears, I can go into the tests for the class in question, read what the class should be allowed to do, and what it shouldn't be allowed to do, so I have a much better knowledge of the system in general.  I like to think of unit tests as comments on steroids, as if written correctly, they can help describe your code as well as help prove that it does what you want it to do.

This is all sounds great and marvelous, however, writing your unit tests in a way that accomplishes, doesn't occur magically.  You will need to define a process/convention to make your tests easy to read and understand.  I've worked on big monolithic projects where reading the tests and figuring out what the heck was going on, took longer than writing the actually tests themselves. When you find yourself in this situation, your tests aren't giving you the benefit they should and a lot of people at that stage tend to think tests are a waste of time.

So, if we want to write easy to understand tests, in a way that makes it easy for developers to read them, you may need to expand your testing toolkit.  This is where Fluent Assertions come in.

What Is Fluent Assertions

Fluent Assertions is a Nuget package I've been using consistently on my projects for about 6 years.  It's extremely simple to pick-up and start using.  Most people can get to grips with it within 5-10 minutes and it will make reading your unit tests a little bit easier.  Fluent Assertions is free so there really isn't a party foul for trying it out.  I think I've introduced Fluent Assertions to over 10 teams now and so far no one's complained.  The biggest reason why most teams don't use it is just lack of exposure to it. Using a standard approach a unit test may look similar to this:

[TestMethod]  
public void Example_test()  
{  
    var result = PerformLogic();
    var expected = true;  
    Assert.AreEqual(expected, actual);  
}  

There's nothing wrong with this test but you need to spend a second or two to understand what's going on. Instead, using FLuent Assertations you can write the same test like this:

[TestMethod]  
public void Example_test()  
{  
    var result = PerformLogic();
    result.Should().BeTrue();  
}  

Hopefully, you can see that the second example takes a lot less time to read, as it reads like a sentence rather than an Assert statement. Fundamentally, this is all Fluent Assertions is, a number of extension methods that make it easier to read your unit tests compared to Assert statements. I'm hoping you can understand why it's so easy to pick up. All you need to do is get the outcome of your test in a result variable, use the Should() exertion and then use Fluent Assertions other extensions to test for your use case. Simple!

Fluent comes with a number of different extensions depending on the data-type
s you are testing against, there's extensions for string, int’s, bool, exceptions, collections, Guides, dates etc.. more information about the extensions can be found here.

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top