是否可以在 IEnumerable上使用 Microsoft.VisualStudio.QualityTools.UnitTesting.CollectionAssert?

发布于 2024-08-17 22:07:03 字数 1027 浏览 3 评论 0原文

我有一个测试场景,我想检查两个集合是否相等。我找到了 Microsoft.VisualStudio.QualityTools.UnitTesting.CollectionAssert 类,但它仅适用于 ICollection。由于我正在测试实体框架的存储库,因此需要比较 IObjectSet,这是行不通的 - IObjectSet 未实现ICollection

有什么方法可以使用此类来比较集合,或者我是否必须创建自己的实现? (为什么 Microsoft 团队不让该类与 IEnumerable 一起使用,因为这是集合的“基本接口”?)

编辑:这是我的测试代码:

// Arrange
var fakeContext = new FakeObjectContext();
var dummies = fakeContext.Dummies;
var repo = new EFRepository<DummyEntity>(fakeContext);

// Act
var result = repo.GetAll();

// Assert
Assert.IsNotNull(result, NullErrorMessage(MethodName("GetAll")));
Assert.IsInstanceOfType(result, typeof(IEnumerable<DummyEntity>), IncorrectTypeMessage(MethodName("GetAll"), typeof(IEnumerable<DummyEntity>)));
CollectionAssert.AreEqual(dummies.ToList(), result.ToList());

最后一行的 CollectionAssert.AreEqual 调用失败,表明索引 0 处的元素不相等。我做错了什么?

I have a testing scenario where I want to check if two collections are equal. I have found the class Microsoft.VisualStudio.QualityTools.UnitTesting.CollectionAssert, but it only works on ICollection<T>. Since I'm testing a repository for Entity Framework, and thus need to compare IObjectSet<T>s, that won't do - IObjectSet<T> doesn't implement ICollection<T>.

Is there any way I can use this class to compare the collecitons, or do I have to create my own implementation? (And why the heck didn't the Microsoft team make the class work with IEnumerable<T> instead, as that is the "base interface" for collections?)

EDIT: This is my test code:

// Arrange
var fakeContext = new FakeObjectContext();
var dummies = fakeContext.Dummies;
var repo = new EFRepository<DummyEntity>(fakeContext);

// Act
var result = repo.GetAll();

// Assert
Assert.IsNotNull(result, NullErrorMessage(MethodName("GetAll")));
Assert.IsInstanceOfType(result, typeof(IEnumerable<DummyEntity>), IncorrectTypeMessage(MethodName("GetAll"), typeof(IEnumerable<DummyEntity>)));
CollectionAssert.AreEqual(dummies.ToList(), result.ToList());

The CollectionAssert.AreEqual call on the last line fails, stating that the elements at index 0 are not equal. What am I doing wrong?

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

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

发布评论

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

评论(2

一个人的旅程 2024-08-24 22:07:03

一个厚脸皮的选项(虽然信息不多)是断言 expected.SequenceEqual(actual) 返回 true

您可以编写一个强制集合的包装方法(每个集合上都有.ToList())?但说实话,你不妨在单元测试代码中调用 .ToList()

CollectionAssert.AreEqual(expected.ToList(), actual.ToList()); // but tidier...

A cheeky option (not quite as much info though) is to assert that expected.SequenceEqual(actual) returns true.

You could write a wrapper method that forces a collection (.ToList() on each)? But to be honest you might as well just call .ToList() in the unit test code:

CollectionAssert.AreEqual(expected.ToList(), actual.ToList()); // but tidier...
忆梦 2024-08-24 22:07:03

如果您要比较结果集,您可能需要使用 CollectionAssert.AreEquivalent 将忽略顺序。您还应该确保已对要比较的元素类型实现了 Equals。

If you're comparing result sets you might want to use CollectionAssert.AreEquivalent which will ignore the order. You should also make sure you have implemented Equals on the type of elements you are comparing.

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