作为替代方案,您可以考虑使用FluentAssertions
单元测试框架,该框架与Microsoft单元测试兼容。
那么你的代码将变成:
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.");
这也符合这样的事情工作:
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.");
如果你改变ints2.Add(1);
到ints2.Add(2);
,单元测试,然后将正确失败。
注意ShouldBeEquivalentTo()
递归下降被比较的对象,并处理集合,因此名单中甚至列出将与它的工作 - 例如:
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.");
Similair的问题在这里:http://stackoverflow.com/questions/5194966/mstest-collectionassert-areequivalent-failed-the-expected-collection-containing –