Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(When) would it be a good idea to use FluentAssertions? [closed]

I am rewriting a C# .NET project and currently planning how I am going to do the testing.

After everything I have read I will install the XUnit framework (for the first time -- I am more experienced with MSTest). Now I am wondering whether I should combine it with FluentAssertions (which I also never used before) or rather write pure XUnit tests.

At a first glance, FluentAssertions sounds nerdy and stylish, but I'm not sure if it really will lead me to write best-readable code and how well it will scale over complex tests.

Hence I am searching for your experience and arguments. [When] (do | would) you use FluentAssertions? I'm curious.

like image 294
KnorxThieus Avatar asked Apr 03 '19 12:04

KnorxThieus


People also ask

What is FluentAssertions?

Fluent Assertions is a set of . NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit test.

Should I use fluent assertions?

Using FluentAssertions instead With FluentAssertions it's much better. The code to be tested for an exception is now placed in an action. The ShouldThrow() extension method executes the action and monitors for the exception. The test now properly fails because GetMember("Nmae").

What is fluent assertions C#?

FluentAssertions is a library that improves unit tests by providing better failure messages, simplifies assertions in many scenarios, and provides a fluent interface (which improves code readability).


2 Answers

Fluent is mostly about readability and convenience.
If you are going to write more than a handful of unit test I'd suggest using it. I recently had the case where I was mapping object 'a' and 'b' onto object 'c' and I wanted to verify the mapper with a unit test.
So, I created an 'expectedObject' which contained all properties that object 'c' should contain once it was mapped.
As I had not written a comparer, nor did I have the need for one, it would have been very cumbersome to compare object 'c' with 'expectedObject' to assert they contain the same data. The object in question contained many properties which in turn had many properties.

But with Fluent I could simply write

c.Should().BeEquivalentTo(expectedObject);

This is much easier to read than a litany of Assert.AreEqual() and in this case, more importantly, much faster to write as well.

like image 93
AirLancer Avatar answered Oct 02 '22 15:10

AirLancer


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 actual = 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!

http://www.jondjones.com/c-sharp-bootcamp/tdd/fluent-assertions/what-is-fluent-assertions-and-should-i-be-using-it

like image 45
Joey Phillips Avatar answered Oct 02 '22 14:10

Joey Phillips