Can Fluent Assertions use a string-insensitive comparison for IEnumerable?

前端 未结 4 1066
渐次进展
渐次进展 2021-01-17 10:46

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 rea

4条回答
  •  礼貌的吻别
    2021-01-17 11:00

    you could wirte a extension method for IEnumerable yourself (this is how I do this stuff) and I thing some Unit-Testframeworks already do so (FSUnit AFAIK)

    Here is a simple example (you could improve a lot - but it should do)

    public static AssertEqualSetCaseInsensitive(this IEnumerable actual, IEnumerable 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);
    

提交回复
热议问题