Is there an easy way in xunit.net to compare two c

2020-03-09 07:23发布

问题:

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.

Thanks for your answers in advance.

回答1:

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.



回答2:

Not a Xunit, but a Linq answer :

bool areSame = !expected.Except(actual).Any() && expected.Count == actual.Count;

So in XUnit :

Assert.True(!expected.Except(actual).Any() && expected.Count == actual.Count));

As @robi-y said, in Microsoft.VisualStudio.QualityTools.UnitTestFramework there is CollectionAssert.AreEquivalent



回答3:

Maybe another way is:

Assert.True(expected.SequenceEqual(actual));

This does checks the order too. This is what happens internally:

using (IEnumerator<TSource> e1 = first.GetEnumerator())
using (IEnumerator<TSource> e2 = second.GetEnumerator())
{
    while (e1.MoveNext())
    {
        if (!(e2.MoveNext() && comparer.Equals(e1.Current, e2.Current))) return false;
    }
    if (e2.MoveNext()) return false;
}
return true;

So if you don't care about the order, just order both lists before:

Assert.True(expected.OrderBy(i => i).SequenceEqual(actual.OrderBy(i => i)));


回答4:

You can use CollectionAssert.AreEquivalent from Microsoft

CollectionAssert.AreEquivalent(expected, actual);


回答5:

This is almost the same as your code. The only simplification is using Assert.Contains instead of Assert.True(expected.Contains(...)).

[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 expected)
        Assert.Contains(item, actual);
}