Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way in xunit.net to compare two collections without regarding the items' order?

In one of my tests, I want to ensure that a collection has certain items. Therefore, I want to compare this collection with the items of an expected collection not regarding the order of the items. Currently, my test code looks somewhat like this:

[Fact] public void SomeTest() {     // Do something in Arrange and Act phase to obtain a collection     List<int> actual = ...      // Now the important stuff in the Assert phase     var expected = new List<int> { 42, 87, 30 };     Assert.Equal(expected.Count, actual.Count);     foreach (var item in actual)         Assert.True(expected.Contains(item)); } 

Is there any easier way to achieve this in xunit.net? I can't use Assert.Equal as this method checks if the order of the items is the same in both collections. I had a look at Assert.Collection but that doesn't remove the Assert.Equal(expected.Count, actual.Count) statement in the code above.

like image 373
feO2x Avatar asked Jan 29 '15 15:01

feO2x


People also ask

What is IClassFixture in xUnit?

Important note: xUnit.net uses the presence of the interface IClassFixture<> to know that you want a class fixture to be created and cleaned up. It will do this whether you take the instance of the class as a constructor argument or not.

How do you assert two objects are equal in xUnit?

You'll have to implement IEquatable<T> for your objects, and then Assert. Equals will work. Assert. Same() compares by reference; it asserts that Obj1 and Obj2 are the same object rather than just looking the same.


1 Answers

Brad Wilson from xunit.net told me in this Github Issue that one should use LINQ's OrderBy operator and afterwards Assert.Equal to verify that two collections contain equal items without regarding their order. Of course, you would have to have a property on the corresponding item class that you can use for ordering in the first place (which I didn't really have in my case).

Personally, I solved this problem by using FluentAssertions, a library that provides a lot of assertion methods that can be applied in a fluent style. Of course, there are also a lot of methods that you can use to validate collections.

In the context of my question, I would use something like the following code:

[Fact] public void Foo() {     var first = new[] { 1, 2, 3 };     var second = new[] { 3, 2, 1 };      first.Should().BeEquivalentTo(second); } 

This test passes because the BeEquivalentTo call ignores the order of the items.

Shouldly is also a good alternative if you do not want to go with FluentAssertions.

like image 177
feO2x Avatar answered Sep 17 '22 17:09

feO2x