这个测试怎么会失败呢?
[TestMethod]
public void Get_Code()
{
var expected = new List<int>();
expected.AddRange(new [] { 100, 400, 200, 900, 2300, 1900 });
var actual = new List<int>();
actual.AddRange(new [] { 100, 400, 200, 900, 2300, 1900 });
Assert.AreEqual(expected, actual);
// Assert.AreSame(expected, actual) fails
// Assert.IsTrue(expected.Equals(actual)) fails
}
我尝试了这个线程中的其他答案,它们对我不起作用,我比较了对象的集合,这些对象的属性中存储了相同的值,但对象是不同的。
方法调用:
CompareIEnumerable(to, emailDeserialized.ToIndividual,
(x, y) => x.ToName == y.ToName && x.ToEmailAddress == y.ToEmailAddress);
比较方法:
private static void CompareIEnumerable<T>(IEnumerable<T> one, IEnumerable<T> two, Func<T, T, bool> comparisonFunction)
{
var oneArray = one as T[] ?? one.ToArray();
var twoArray = two as T[] ?? two.ToArray();
if (oneArray.Length != twoArray.Length)
{
Assert.Fail("Collections are not same length");
}
for (int i = 0; i < oneArray.Length; i++)
{
var isEqual = comparisonFunction(oneArray[i], twoArray[i]);
Assert.IsTrue(isEqual);
}
}
List<AdminUser> adminDetailsExpected = new List<AdminUser>()
{
new AdminUser {firstName = "test1" , lastName = "test1" , userId =
"001test1" },
new AdminUser {firstName = "test2" , lastName = "test2" , userId =
"002test2" }
};
/ /行为
List<AdminUser> adminDetailsActual = RetrieveAdmin(); // your retrieve logic goes here
/ /维护
Assert.AreEqual(adminDetailsExpected.Count, adminDetailsActual.Count); //Test succeeds if the count matches else fails. This count can be used as a work around to test