作爲替代方案,您可以考慮使用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 –