(何时)使用FluentAssertions是一个好主意? [英] (When) would it be a good idea to use FluentAssertions?

查看:46
本文介绍了(何时)使用FluentAssertions是一个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重写 C#.NET项目,目前正在计划如何进行测试.

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

阅读完所有内容后,我将安装 XUnit框架(第一次-我对MSTest更有经验).现在,我想知道是否应该将它与 FluentAssertions (我也从未使用过)结合,或者应该编写纯XUnit测试.

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.

乍一看,FluentAssertions听起来很书呆子和时尚,但是我不确定它是否真的会导致我编写最佳可读的代码以及它在复杂测试中的扩展程度.

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.

因此,我正在寻找您的经验和观点.[何时](使用|将使用)FluentAssertions?我很好奇.

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

推荐答案

Fluent主要是关于可读性和便利性.
如果您打算编写不止几个单元测试,我建议您使用它.最近,我遇到了将对象'a'和'b'映射到对象'c'的情况,我想通过单元测试来验证映射器.
因此,我创建了一个"expectedObject",其中包含对象"c"在映射后应包含的所有属性.
由于我没有编写比较器,也没有必要使用比较器,因此比较对象'c'和'expectedObject'来断言它们包含相同的数据将非常麻烦.有问题的对象包含许多属性,而这些属性又具有许多属性.

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.

但是有了Fluent,我可以简单地写

But with Fluent I could simply write

c.Should().BeEquivalentTo(expectedObject);

这比一连串的 Assert.AreEqual()更容易阅读,在这种情况下,更重要的是,它的编写速度也更快.

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

这篇关于(何时)使用FluentAssertions是一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆