xUnit : 断言两个 List; 是平等的吗?

发布于 2024-07-10 22:19:55 字数 461 浏览 10 评论 0原文

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

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

是否有我可以使用的 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);

有这样的事情吗?

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

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨后彩虹 2024-07-17 22:19:55

2021 年 4 月 1 日更新

我建议使用 FluentAssertions。 它拥有一个庞大的 IntelliSense 友好断言库,适用于许多用例,包括 集合

collection.Should().Equal(1, 2, 5, 8);
collection.Should().NotEqual(8, 2, 3, 5);
collection.Should().BeEquivalentTo(8, 2, 1, 5);

原始答案

xUnit.Net< /strong> 识别集合,因此您只需要做

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

您可以在 CollectionAsserts.cs

对于 NUnit 库集合比较方法是

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

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

更多详细信息请参见:CollectionAssert

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

2021-Apr-01 update

I recommend using FluentAssertions. It has a vast IntelliSense-friendly assertion library for many use cases including collections

collection.Should().Equal(1, 2, 5, 8);
collection.Should().NotEqual(8, 2, 3, 5);
collection.Should().BeEquivalentTo(8, 2, 1, 5);

Original answer

xUnit.Net recognizes collections so you just need to do

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

You can see other available collection assertions in CollectionAsserts.cs

For NUnit library collection comparison methods are

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

and

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

More details here: CollectionAssert

MbUnit also has collection assertions similar to NUnit: Assert.Collections.cs

长伴 2024-07-17 22:19:55

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

Assert.Equal(预期,实际);

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

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

Assert.Equal(expected, actual);

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.

貪欢 2024-07-17 22:19:55

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

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

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

With xUnit, should you want to cherry pick properties of each element to test you can use 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.

遗失的美好 2024-07-17 22:19:55

在当前版本的 xUnit v2.4.2 中,您还可以使用:

Assert.Equivalent(expected: ..., actual: ..., strict: true);

strict 参数通过要求 expected 列表中的所有值都在 actual 列表中(意味着 expected : 当 strictfalse 时,[1, 2] 相当于 actual: [2, 3, 1],但不是当 stricttrue 时等效)。

In the current version of xUnit v2.4.2 you can also use:

Assert.Equivalent(expected: ..., actual: ..., strict: true);

The strict parameter influences this comparison by requiring that all values in the expected list are in the actual list (meaning expected: [1, 2] is equivalent to actual: [2, 3, 1] when strict is false, but not equivalent when strict is true).

不气馁 2024-07-17 22:19:55

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

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

  1. 定义 IEqualityComparer

    的实现

  2. 将比较器实例作为第三个参数传递到 Assert.True 方法中:

    Assert.True(预期、实际、new MyEqualityComparer());

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

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

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

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

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.True method:

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

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

// 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.

并安 2024-07-17 22:19:55

我找到了断言两个列表相等的最佳方法

Assert.All(actual.Items,item => Source.Contains(item));

i found best way for assert equallity two list

Assert.All(actual.Items,item => Source.Contains(item));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文