简体   繁体   English

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

[英]Can Fluent Assertions use a string-insensitive comparison for IEnumerable<string>?

I've got a pair of Lists I'm trying to compare using Fluent Assertions. 我有一对列表,我试图使用Fluent断言进行比较。 I can code up a comparison easily, but I'd like to use Fluent Assertions so that I can get the reason to show up in the test failed message. 我可以轻松编写比较代码,但我想使用Fluent Assertions,以便我可以在测试失败消息中显示出来的原因。

Everything I've seen so far seems to using the default Object.Equals comparison, which is case-sensitive. 到目前为止我看到的所有内容似乎都使用默认的Object.Equals比较,它区分大小写。 I can't seem to pass an IComparer to the Equal or Contains methods, so is there any other way? 我似乎无法将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);
}

In later versions of FluentAssetrions one can use following: 在FluentAssetrions的更高版本中,可以使用以下内容:

stringValue.Should().BeEquivalentTo(stringToCompare);

Summary from [metadata]: [元数据]摘要:

    // Summary:
    //     Asserts that a string is exactly the same as another string, including any
    //     leading or trailing whitespace, with the exception of the casing.

Works in version that I use (FluentAssertions.2.2.0.0). 适用于我使用的版本(FluentAssertions.2.2.0.0)。

We could add an optional lambda expression to the Equal() method. 我们可以在Equal()方法中添加一个可选的lambda表达式。 Then, you could do something like 然后,你可以做类似的事情

[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))
} 

A IComparer would also be possible, but I think the occasional exception to Equal()'s default behavior wouldn't warrant an additional custom-written class. IComparer也是可能的,但我认为Equal()默认行为的偶然异常不能保证额外的自定义编写类。 In fact, a separate IComparer might ever obscure the intention of the test. 实际上,单独的IComparer可能会模糊测试的意图。 Let me know what you guys think is the best solution, so I can add it as an issue on Codeplex for release 1.8.0. 让我知道你们认为什么是最好的解决方案,所以我可以在Codeplex 1.8.0版本上添加它作为一个问题。

How about adding a new Fluent assertion via an extention method (or two)? 如何通过扩展方法(或两个)添加新的Fluent断言? I've written code to add a .EqualInsensitively(...) to the available fluent assertions for a collection of strings. 我编写了代码,将.EqualInsensitively(...)添加到字符串集合的可用流畅断言中。

I've put the code to implement this on an external pastebin because its a little long and the MS-PL might not be compatible with CC-Wiki. 我已经把代码放在外部pastebin上实现这个,因为它有点长,MS-PL可能与CC-Wiki不兼容。

Use something like this: 使用这样的东西:

private static void Main()
{
    var mylist = new List<string> {"abc", "DEF", "GHI"};
    mylist.Should().EqualInsensitively(new[] {"AbC", "def", "GHI"})
      .And.NotContain(string.Empty); //Emaple of chaining
}

you could wirte a extension method for IEnumerable<string> yourself (this is how I do this stuff) and I thing some Unit-Testframeworks already do so (FSUnit AFAIK) 你可以自己编写一个IEnumerable<string>的扩展方法(这就是我这样做的东西)而且我的一些Unit-Testframeworks已经这样做了(FSUnit AFAIK)

Here is a simple example (you could improve a lot - but it should do) 这是一个简单的例子(你可以改进很多 - 但它应该这样做)

public static AssertEqualSetCaseInsensitive(this IEnumerable<string> actual, IEnumerable<string> expected)
{
   if (actual.Count() != expected.Count())
      Assert.Fail("not same number of elements");

   if (!actual.All(a => expected.Any(e => e.ToLower() == a.ToLower()))
      Assert.Fail("not same sets");
}

just use like 就像使用一样

actual.AssertEqualSetCaseInsensitive(expected);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM