简体   繁体   English

单元测试-如何比较两个分页的集合以声明所有不同的项目?

[英]Unit Testing - How to Compare Two Paged Collections To Assert All Items Different?

Had a quick look here, couldn't find a duplicate (correct me if im wrong). 在这里快速浏览,找不到重复的内容(如果我错了,请纠正我)。

I've got the following Unit Test for some Paging with LINQ: 对于LINQ的一些分页,我具有以下单元测试:

    // Arrange.
    const int locationId = 1;
    const LocationType locationType = LocationType.City;
    int pageSize = 10;

    // Act.
    var postsPageOne = PostService.FindAllPostsForLocationPaged<Review>(locationId, locationType, 1, pageSize);
    var postsPageTwo = PostService.FindAllPostsForLocationPaged<Review>(locationId, locationType, 2, pageSize);

    // Assert.
    Assert.IsTrue(postsPageOne.Count > 0);
    Assert.IsTrue(postsPageTwo.Count > 0);
    Assert.AreEqual(postsPageOne.Count, pageSize);
    Assert.AreEqual(postsPageTwo.Count, pageSize);
    CollectionAssert.AllItemsAreNotNull(postsPageOne.ToArray());
    CollectionAssert.AllItemsAreNotNull(postsPageTwo.ToArray());

I want to assert that all items in the collection postsPageOne are different to all the items in the collection postsPageTwo . 我想断言,在收集postsPageOne所有项目都在收集postsPageTwo所有项目不同。 (seems like the way to test paging) (好像是测试分页的方法)

Any ideas of how I can do that? 关于我该怎么做的任何想法?

That doesn't sound like the way to test paging to me. 这听起来不像是向我测试分页的方法。 I would test paging by having a known dataset, and checking that each page does contain the data I'd expect it to. 我将通过拥有一个已知的数据集来测试分页,并检查每个页面是否确实包含我期望的数据。 Otherwise your code could just make up random data, and so long as it was different, your test would pass. 否则,您的代码只能组成随机数据,只要它们不同,测试就可以通过。

Use a small page size (eg 2 or 3) to keep the amount of data to test nice and small. 使用较小的页面大小(例如2或3)以保持测试的数据量的大小。

Assert.IsFalse(postsPageOne.Intersect(postsPageTwo).Any());

Assuming an Id property on your posts: 假设您的帖子具有Id属性:

!postsPageOne.Select(x => x.Id).Intersect(postsPageTwo.Select(x => x.Id)).Any()

The way I test paging in my integration tests is to request a large, single-page list, and then break it into pages, requesting each page and keeping track of all IDs in the order they're returned. 我在集成测试中测试分页的方法是请求一个大的单页列表,然后将其分成多个页面,请求每个页面并按照返回的顺序跟踪所有ID。 Then I compare the IDs returned in the large list to the IDs returned by the paging calls and make sure they match exactly. 然后,我将大列表中返回的ID与分页调用返回的ID进行比较,并确保它们完全匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM