Fluent Assertions可以对IEnumerable <string>使用字符串不敏感的比较吗?

Zug*_*gbo 13 c# unit-testing fluent-assertions

我有一对列表,我试图使用Fluent断言进行比较.我可以轻松编写比较代码,但我想使用Fluent Assertions,以便我可以在测试失败消息中显示出来的原因.

到目前为止我看到的所有内容似乎都使用默认的Object.Equals比较,它区分大小写.我似乎无法将IComparer传递给Equal或Contains方法,那么还有其他方法吗?

[TestMethod()]
public void foo()
{
  var actual = new List<string> { "ONE", "TWO", "THREE", "FOUR" };
  var expected = new List<string> { "One", "Two", "Three", "Four" };

  actual.Should().Equal(expected);
}
Run Code Online (Sandbox Code Playgroud)

Tar*_*asB 26

在FluentAssetrions的更高版本中,可以使用以下内容:

stringValue.Should().BeEquivalentTo(stringToCompare);
Run Code Online (Sandbox Code Playgroud)

[元数据]摘要:

    // Summary:
    //     Asserts that a string is exactly the same as another string, including any
    //     leading or trailing whitespace, with the exception of the casing.
Run Code Online (Sandbox Code Playgroud)

适用于我使用的版本(FluentAssertions.2.2.0.0).


Den*_*men 3

我们可以向 Equal() 方法添加一个可选的 lambda 表达式。然后,你可以做类似的事情

[TestMethod()] 
public void foo() 
{ 
   var actual = new List<string> { "ONE", "TWO", "THREE", "FOUR" }; 
   var expected = new List<string> { "One", "Two", "Three", "Four" }; 

  actual.Should().Equal(expected, 
    (o1, o2) => string.Compare(o1, o2, StringComparison.InvariantCultureIgnoreCase))
} 
Run Code Online (Sandbox Code Playgroud)

IComparer 也是可能的,但我认为 Equal() 默认行为的偶尔例外不会保证额外的自定义编写的类。事实上,单独的 IComparer 可能会掩盖测试的意图。让我知道你们认为最好的解决方案是什么,这样我就可以将其添加为 Codeplex 1.8.0 版本的问题。