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

旧街凉风 提交于 2021-01-04 02:39:21

问题


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.


回答1:


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.




回答2:


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!

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



来源:https://stackoverflow.com/questions/55495026/when-would-it-be-a-good-idea-to-use-fluentassertions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!