Is it possible to compare two collections regardless the items order using Should().BeEquivalentTo() with showing the concrete items are missing or surplus on the actual comparing the expected collection? Or is any different method more suitable for this purpose?
Both
Should().BeEquivalentTo()
or explicitly
Should().BeEquivalentTo(expected, options => options.WithoutStrictOrdering());
or with some AI help (because I am not so good at it :)
Should().BeEquivalentTo(expected, options => options
.WithoutStrictOrdering()
.Using<int>(ctx => ctx.Subject.Should().Be(ctx.Expectation)
.WhenTypeIs<int>());
constructs gave me the same results, which only shows the all items in the actual and expected collection and say that they differs in the length.
In case of collections with the same size, it looks quite ok. It shows the different items and their indexes (and which is not so necessary without strict ordering), it shows the different item behind the particular index on the expected collection - because if I don't care about the order I don't need what is behind the particular index at the counter side. example:
[Fact]
public void TestCollectionsEquivalentSameLength()
{
var expected = new[] { 2, 1, 3, 5 };
var actual = new[] { 1, 2, 4, 6 };
actual.Should().BeEquivalentTo(expected, options => options.WithoutStrictOrdering());
}
It shows: 'Expected actual[2] to be 3, but found 4. Expected actual[3] to be 5, but found 6.'
So, Instead I would see better to just say (even with keeping the indexes info) "Same item count; actual[2] to be 3, actual[3] to be 5 are surplus to expected; expected[2] to be 5, expected[2] to be 6 are missing at the actual"
Comparison of collections with different size produces lesser suitable info in the messages for my purpose ...
For example, if the comparing collections will be changed to following:
var expected = new[] { 2, 1, 3, 5 };
var actual = new[] { 1, 2, 4, 6, 5, 7 };
It shows: 'Expected actual to be a collection with 4 item(s), but {1, 2, 4, 6, 5, 7} contains 2 item(s) more than {2, 1, 3, 5}.'
Well, is it possible to customize the message in this case to show something like: "actual has 2 more items; actual[2] to be 4, actual[3] to be 6,actual[7] to be 7 are surplus to expected; expected[2] to be 3, is missing at the actual"
What I have currently found so far, is a solution using different constructs.
[Fact]
public void TestCollectionsNewEquivMoreItems()
{
var expected = new[] { 2, 1, 3, 5 };
var actual = new[] { 1, 2, 4, 6, 5, 7 };
using (new AssertionScope())
{
actual.Should().Contain(expected);
expected.Should().Contain(actual);
}
}
It shows: 'Expected actual {1, 2, 4, 6, 5, 7} to contain {2, 1, 3, 5}, but could not find {3}. Expected expected {2, 1, 3, 5} to contain {1, 2, 4, 6, 5, 7}, but could not find {4, 6, 7}.'
(I am satisfied with the info about missing items. Only one small cons is, that the second part is formulated from the perspective of expected).
Some notes at the end:
- please excuse me, this text might not be grammatically correct
- I don't insist to show the index info