作为替代方案,您可以考虑使用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.");