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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With