Unit Tests are useful. Almost every developer, with a least some experience in commercial software development, will agree with this statement, in general. Unfortunately, far less developers will agree that Unit Tests are useful enough to spend additional time and efforts to write them. In this situation, any tools or practices, which can simplify unit testing are welcome.

FluentAssertion library is an example of such tool. Goal of this library is to simplify the assertion part of the unit test by providing a more expressive way to define the asserts and by reporting the assertion failures in a friendly way.

Here is a basic example

double result = 19.99;
result.Should().BeInRange(99.99, 199.99, "because we filtered values for this range");

It demonstrates an expressive, highly readable syntax of FluentAssertion. This test will, obviously, fail and it will do it with the following friendly message.

Expected value to be between 99.99 and 199.99 because we filtered values for this range, but found 19.99

As it is expected from the any assertion library, FluentAssertions can handle exceptions, combinations of conditions and validating complex objects. It can also can validate metadata, events, XML and execution time.

Here is a more complicated sample where a Customer object compared to a CustomerDTO. All properties existing in both classes should match, all nested objects should be ignored, as well as all public fields

customer.ShouldBeEquivalentTo(customerDto,
                options => options.ExcludingFields()
                    .IncludingProperties()
                    .ExcludingNestedObjects());

This is just a short list of the library features, and to not repeat the documentation, visit FluentAssertions web site.

blog comments powered by Disqus