Fluent断言可以对IEnumerable< string>使用字符串不敏感的比较吗? [英] Can Fluent Assertions use a string-insensitive comparison for IEnumerable<string>?

查看:87
本文介绍了Fluent断言可以对IEnumerable< string>使用字符串不敏感的比较吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表,我正在尝试使用Fluent断言进行比较.我可以轻松地编写一个比较代码,但是我想使用Fluent断言,这样我就可以找到出现在测试失败消息中的原因.

I've got a pair of Lists I'm trying to compare using Fluent Assertions. 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.

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

Everything I've seen so far seems to using the default Object.Equals comparison, which is case-sensitive. I can't seem to pass an IComparer to the Equal or Contains methods, so is there any other way?

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

推荐答案

我们可以向Equal()方法添加一个可选的lambda表达式.然后,您可以执行

We could add an optional lambda expression to the Equal() method. 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))
} 

也可以使用IComparer,但我认为Equal()的默认行为偶尔会出现异常,因此不需要额外的自定义编写类.实际上,单独的IComparer可能会掩盖测试的意图.让我知道你们认为是最好的解决方案,因此我可以将其作为Codeplex版本1.8.0的一个问题添加.

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. In fact, a separate IComparer might ever obscure the intention of the test. 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.

这篇关于Fluent断言可以对IEnumerable&lt; string&gt;使用字符串不敏感的比较吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆