As an alternative, you could consider using the FluentAssertions
Unit Test framework, which is compatible with Microsoft Unit Test.
Then your code would become:
var x = new List<object>() { new List<int>() };
var y = new List<object>() { new List<int>() };
x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response.");
It would also work with this sort of thing:
var ints1 = new List<int>();
var ints2 = new List<int>();
ints1.Add(1);
ints2.Add(1);
var x = new List<object>() { ints1 };
var y = new List<object>() { ints2 };
x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response.");
If you changed ints2.Add(1);
to ints2.Add(2);
, the unit test would then correctly fail.
Note that ShouldBeEquivalentTo()
recursively descends the objects being compared, and handles collections, so even lists of lists will work with it - for example:
var ints1 = new List<int>();
var ints2 = new List<int>();
ints1.Add(1);
ints2.Add(1); // Change this to .Add(2) and the unit test fails.
var objList1 = new List<object> { ints1 };
var objList2 = new List<object> { ints2 };
var x = new List<object> { objList1 };
var y = new List<object> { objList2 };
x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response.");