在 NUnit 中断言集合与预期集合的顺序相同

发布于 2024-07-23 10:53:59 字数 154 浏览 3 评论 0原文

我知道如何检查集合是否按某些属性排序:

Assert.That(actual, Is.Ordered.By("Foo"));

我如何断言实际按此特定顺序包含元素(1,2,5,3,4)(无需编写自定义比较器)。

I know how to check that a collection is ordered by some property:

Assert.That(actual, Is.Ordered.By("Foo"));

How can I assert that actual contains the elements (1,2,5,3,4) in this specific order (without writing a custom comparer).

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

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

发布评论

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

评论(1

失眠症患者 2024-07-30 10:53:59

用途

CollectionAssert.AreEqual(expectedIEnumerable, actualIEnumerable);

这会检查项目是否相等且顺序相同。

我相当确定,当您在集合上使用 Assert.That 时,您将获得集合断言功能。 因此,您可以说诸如

Assert.That(collection, Is.EqualTo(expectedCollection)); // Same order

Assert.That(collection, Is.EquivalentTo(expectedCollection)); // Same item count

之类的内容以及诸如

Assert.That(collection, Has.Count.EqualTo(expectedSize));

Has 关键字之类的内容,这样您就可以了解特定于集合断言的内容,并且非常有用。

Use

CollectionAssert.AreEqual(expectedIEnumerable, actualIEnumerable);

This checks that the items are equal and are in the same order.

I'm fairly sure that when you use Assert.That on a collection, you get collection assert functionality. So you can say stuff like

Assert.That(collection, Is.EqualTo(expectedCollection)); // Same order

or

Assert.That(collection, Is.EquivalentTo(expectedCollection)); // Same item count

as well as stuff like

Assert.That(collection, Has.Count.EqualTo(expectedSize));

The Has keyword opens you up to the stuff that was specific to collection asserts, and is really useful.

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