C# xUnit :断言两个 List<T> 是否相等?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/419659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:41:03  来源:igfitidea点击:

xUnit : Assert two List<T> are equal?

c#xunit

提问by Petar Petrov

I'm new to TDD and xUnit so I want to test my method that looks something like:

我是 TDD 和 xUnit 的新手,所以我想测试我的方法,如下所示:

List<T> DeleteElements<T>(this List<T> a, List<T> b);

Is there any Assert method that I can use ? I think something like this would be nice

有没有我可以使用的 Assert 方法?我认为这样的事情会很好

List<int> values = new List<int>() { 1, 2, 3 };
List<int> expected = new List<int>() { 1 };
List<int> actual = values.DeleteElements(new List<int>() { 2, 3 });

Assert.Exact(expected, actual);

Is there something like this ?

有这样的吗?

采纳答案by Konstantin Spirin

xUnit.Netrecognizes collections so you just need to do

xUnit.Net识别集合,所以你只需要做

Assert.Equal(expected, actual); // Order is important

You can see other available collection assertions in CollectionAsserts.cs

您可以在CollectionAsserts.cs 中查看其他可用的集合断言

For NUnitlibrary collection comparison methods are

对于NUnit库集合比较方法是

CollectionAssert.AreEqual(IEnumerable, IEnumerable) // For sequences, order matters

and

CollectionAssert.AreEquivalent(IEnumerable, IEnumerable) // For sets, order doesn't matter

More details here: CollectionAssert

更多细节在这里:CollectionAssert

MbUnitalso has collection assertions similar to NUnit: Assert.Collections.cs

MbUnit也有类似于 NUnit 的集合断言:Assert.Collections.cs

回答by hwiechers

In the current version of XUnit (1.5) you can just use

在当前版本的 XUnit (1.5) 中,您可以使用

Assert.Equal(expected, actual);

Assert.Equal(预期,实际);

The above method will do an element by element comparison of the two lists. I'm not sure if this works for any prior version.

上述方法将对两个列表进行逐个元素的比较。我不确定这是否适用于任何以前的版本。

回答by Drew Williams

With xUnit, should you want to cherry pick properties of each element to test you can use Assert.Collection.

使用 xUnit,如果您想挑选每个元素的属性进行测试,您可以使用 Assert.Collection。

Assert.Collection(elements, 
  elem1 => Assert.Equal(expect1, elem1.SomeProperty),
  elem2 => { 
     Assert.Equal(expect2, elem2.SomeProperty);
     Assert.True(elem2.TrueProperty);
  });

This tests the expected count and ensures that each action is verified.

这会测试预期计数并确保每个操作都经过验证。

回答by Dmitry Stepanov

Recently, I was using xUnit 2.4.0and Moq 4.10.1packages in my asp.net core 2.2 app.

最近,我在我的 asp.net core 2.2 应用程序中使用xUnit 2.4.0Moq 4.10.1打包。

In my case I managed to get it work with two steps process:

就我而言,我设法通过两个步骤使其工作:

  1. Defining an implementation of IEqualityComparer<T>
  2. Pass the comparer instance as a third parameter into Assert.Truemethod:

    Assert.True(expected, actual, new MyEqualityComparer());

  1. 定义一个实现 IEqualityComparer<T>
  2. 将比较器实例作为第三个参数传递给Assert.True方法:

    Assert.True(expected, actual, new MyEqualityComparer());

But there is another nicer way to get the same result using FluentAssertionspackage. It allows you to do the following:

但是还有另一种更好的方法可以使用FluentAssertions包获得相同的结果。它允许您执行以下操作:

// Assert          
expected.Should().BeEquivalentTo(actual));

Interestingly that Assert.Equal()always fails even when I ordered the elements of two lists to get them in the same order.

有趣的是,Assert.Equal()即使我对两个列表的元素进行排序以使它们以相同的顺序排列,它也总是失败。