【问题标题】:How to compare case insensitive string using FluentAssertions? C# [duplicate]如何使用 FluentAssertions 比较不区分大小写的字符串? C# [重复]
【发布时间】: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


【解决方案1】:

你可以使用

symbol.ToLower().Should().Be(expectedSymbol.ToLower());

BeEquivalentTo代替Be

symbol.Should().BeEquivalentTo(expectedSymbol);

BeEquivalentTo 元数据状态

断言一个字符串与另一个字符串完全相同,包括任何前导或尾随空格,但大小写除外。

【讨论】:

  • 这很好,但是正如我在问题中添加的那样,土耳其文化将失败。
  • 然后使用重复问题接受的答案。 symbol.Should().Equal(expectedSymbol, (o1, o2) => string.Compare(o1, o2, StringComparison.InvariantCultureIgnoreCase))
猜你喜欢
  • 2012-02-29
  • 2014-06-20
  • 1970-01-01
  • 2013-07-22
  • 2014-07-19
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多