【发布时间】:2018-06-08 21:02:03
【问题描述】:
如何使用 FluentAssertions 轻松比较不区分大小写的字符串?
类似:
symbol.Should().Be(expectedSymbol, StringComparison.InvariantCultureIgnoreCase);
编辑:关于可能的重复和代码:
symbol.Should().BeEquivalentTo(expectedSymbol);
它正在使用 CurrentCulture 进行比较。它会在土耳其文化等情况下刹车。在哪里
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR", false);
string upper = "in".ToUpper(); // upper == "İN"
"in".Should().BeEquivalentTo("In"); // It will fail
所以“StringComparison.InvariantCultureIgnoreCase”部分在这里至关重要。
【问题讨论】:
-
为什么不直接在要比较的字符串上调用
.ToLower()? -
@Glubus 正在寻找更好的方法。请注意,当您使用
ToLower()并且它失败时,FluentAssertions 将报告更改的值(小写)。
标签: c# fluent-assertions