FluentAssertions:排序列表的等效项 [英] FluentAssertions: equivalence of sorted lists

查看:84
本文介绍了FluentAssertions:排序列表的等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#中的FluentAssertions建立两个列表的等价关系,其中有两点很重要:

I'm trying to establish equivalence of two lists using FluentAssertions in C#, where two things are of importance:

  1. 将元素通过其持有的值进行比较,而不是通过引用进行比较(即,它们是等效的,而不是相等的)
  2. 列表中元素的顺序很重要

在FluentAssertions(甚至NUnit)中没有执行此操作的功能吗?

Is there no function in FluentAssertions (or even NUnit) that does this?

干杯!

推荐答案

默认情况下,ShouldBeEquivalentTo()将忽略集合中的顺序,因为在大多数情况下,如果两个集合以任意顺序包含相同的项目,则它们是等效的.如果您确实在意顺序,只需在options =>参数上使用WithStrictOrdering()的重载之一.

By default, ShouldBeEquivalentTo() will ignore the order in the collections because in most cases two collections are equivalent if they contain the same items in any order. If you do care about the order, just use one of the overloads of WithStrictOrdering() on the options => parameter.

示例:

var myList = Enumerable.Range(1, 5);
var expected = new[]
{
    1, 2, 3, 4, 5
};

//succeeds
myList.ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());

//fails
myList.Reverse().ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());

文档中了解有关这些选项的更多信息.

Read more about these options in the documentation.

这篇关于FluentAssertions:排序列表的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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