Search code examples
c#linqunit-testingpaginationassert

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:

    // 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. (seems like the way to test paging)

Any ideas of how I can do that?


Solution

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