我正在重写一个C# .NET 项目,目前正在计划如何进行测试。
在我阅读完所有内容之后,我将安装XUnit 框架(这是第一次——我对 MSTest 更有经验)。现在我想知道我是否应该将它与FluentAssertions(我以前也从未使用过)结合起来,或者更确切地说是编写纯 XUnit 测试。
乍一看,FluentAssertions 听起来很书呆子和时尚,但我不确定它是否真的会让我编写可读性最好的代码,以及它在复杂测试中的扩展性如何。
因此,我正在寻找您的经验和论点。[何时](会 | 会)您使用 FluentAssertions?我很好奇。
我想使用 .NET Standard 实现以下方法:
public static void SetFlag<TEnum>(ref TEnum value, TEnum flag)
where TEnum : Enum
Run Code Online (Sandbox Code Playgroud)
我花了几个小时试图实现这一目标:
|对于像 s 这样的基本类型来说,通过反射获取运算符似乎是不可能的enum。dynamic需要引用额外的包 ( Microsoft.CSharp.RuntimeBinder),但我希望我的库保持纯粹的 .NET 标准符合。我最新的想法是手动比较 TEnum每个有效的枚举类型{byte , sbyte, short, ushort, int, uint, long, ulong}。但这感觉真的很奇怪而且肮脏:
try
{
var v = (byte)(object)value | (byte)(object)flag;
value = (TEnum)(object)v;
return;
}
catch (InvalidCastException) { }
try
{
var v = (int)(object)value | (int)(object)flag; …Run Code Online (Sandbox Code Playgroud) 给定以下输入:
var customers = new[] {
new Customer { Name = "John", Age = 42 },
new Customer { Name = "Mary", Age = 43 }
};
var employees = new[] {
new Employee { FirstName = "John", Age = 42 },
new Employee { FirstName = "Mary", Age = 43 }
};
Run Code Online (Sandbox Code Playgroud)
使用 FluentAssertions 比较这些列表的最佳方法是什么?
我目前唯一的方法是这样的——与Enumerable.SequenceEqual非常相似:
using (var customerEnumerator = customers.GetEnumerator())
using (var employeeEnumerator = employees.GetEnumerator())
{
while (customerEnumerator.MoveNext())
{
employeeEnumerator.MoveNext().Should().BeTrue();
var (customer, employee) = (customerEnumerator.Current, employee.Current); …Run Code Online (Sandbox Code Playgroud) 我在 git 上的提交消息通常如下所示:
* Fix bug abc
* Refactor xyz
* Document 123
* ...
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是一种不好的做法,我应该更喜欢单行提交消息或至少提供一个标题行。
另一方面,这写起来不太舒服......
你有什么建议?
c# ×3
.net ×2
unit-testing ×2
xunit ×2
assertion ×1
conventions ×1
enums ×1
git ×1
git-commit ×1
reflection ×1