Hello,
I am trying to compare the contents of 2 collections in a unit test in .NET using MSTEST. To make things simple, instead of having to .Sort() and then loop through the collection and compare items one at a time, I've found the new and very cool .Intersect Extension method.
It seems to work great by doing:
Assert.AreEqual(expected.Count, actual.Intersect(expected).Count)
However, now that I have a test which needs to be case-sensitive, it breaks. I've tried sending Intersect's second parameter StringComparer.Ordinal,StringComparer.InvariantCulture, and StringComparer.CurrentCulture... no luck..
Anyone experience this before?
thanks!
EDIT: here is the data:
Actual:
(0) "FOO" String
(1) "foo" String
(2) "Foo" String
(3) "fOo" String
(4) "foO" String
(5) "BAR" String
(6) "BAR" String
(7) "BAZ" String
(8) "baz" String
(9) "foo" String
Expected:
(0) "FOO" String
(1) "foo" String
(2) "Foo" String
(3) "fOo" String
(4) "foO" String
(5) "BAR" String
(6) "BAR" String
(7) "BAZ" String
(8) "baz" String
(9) "foo" String
actual.Intersect(expected, StringComparer.CurrentCulture)
(0) "FOO" String
(1) "foo" String
(2) "Foo" String
(3) "fOo" String
(4) "foO" String
(5) "BAR" String
(6) "BAZ" String
(7) "baz" String
It seems to be removing a matching duplicate 'foo', and a matching duplicate 'BAZ'. Perhaps there is a better way to assert collections are matching?
_EDIT2: I think Intersect() removes duplicates, which is why this is breaking. I Found the CollectionAssert Class. This is exactly what I needed! Thanks! _