206

这个测试怎么会失败?

[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
}
4

7 回答 7

420

要对集合进行断言,您应该使用CollectionAssert

CollectionAssert.AreEqual(expected, actual);

List<T>不会覆盖Equals,所以如果Assert.AreEqual只是调用Equals,它将最终使用引用相等。

于 2012-06-15T17:43:20.827 回答
40

我想这会有所帮助

Assert.IsTrue(expected.SequenceEqual(actual));
于 2012-06-15T17:47:22.413 回答
19

如果要检查每个是否包含相同的值集合,则应使用:

CollectionAssert.AreEquivalent(expected, actual);

编辑:

“如果两个集合具有相同数量的相同元素,则它们是等价的,但如果它们的值相等,则元素相等,而不是如果它们引用同一个对象。” - https://msdn.microsoft.com/en-us/library/ms243779.aspx

于 2017-08-14T09:56:55.220 回答
17

我在这个线程中尝试了其他答案,但它们对我不起作用,我正在比较在其属性中存储了相同值但对象不同的对象集合。

方法调用:

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);
        }
    }
于 2014-07-15T12:26:13.593 回答
6

这个测试比较一个日期输入,检查它是否是闰年,如果是,从输入的日期输出 20 个闰年,如果不是,输出 NEXT 20 个闰年,myTest.Testing 引用 myTest 实例,该实例又调用值来自名为测试的列表,其中包含所需的计算值。我必须做的练习的一部分。

[TestMethod]
        public void TestMethod1()
        {
            int testVal = 2012;
            TestClass myTest = new TestClass();
            var expected = new List<int>();
            expected.Add(2012);
            expected.Add(2016);
            expected.Add(2020);
            expected.Add(2024);
            expected.Add(2028);
            expected.Add(2032);
            expected.Add(2036);
            expected.Add(2040);
            expected.Add(2044);
            expected.Add(2048);
            expected.Add(2052);
            expected.Add(2056);
            expected.Add(2060);
            expected.Add(2064);
            expected.Add(2068);
            expected.Add(2072);
            expected.Add(2076);
            expected.Add(2080);
            expected.Add(2084);
            expected.Add(2088);
            var actual = myTest.Testing(2012);
            CollectionAssert.AreEqual(expected, actual);
        }
于 2013-06-13T07:50:25.380 回答
4

Fluent assertions 对数组进行深度比较actualArray.Should().BeEquivalentTo(expectedArray)

于 2020-01-03T16:21:03.410 回答
0
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
于 2019-12-20T12:33:15.623 回答