How to compare Lists in Unit Testing

后端 未结 7 1516
闹比i
闹比i 2020-11-30 00:24

How can this test fail?

[TestMethod]
public void Get_Code()
{
    var expected = new List();
    expected.A         


        
相关标签:
7条回答
  • 2020-11-30 00:35

    I guess this will help

    Assert.IsTrue(expected.SequenceEqual(actual));
    
    0 讨论(0)
  • 2020-11-30 00:41

    To make assertions about collections, you should use CollectionAssert:

    CollectionAssert.AreEqual(expected, actual);
    

    List<T> doesn't override Equals, so if Assert.AreEqual just calls Equals, it will end up using reference equality.

    0 讨论(0)
  • 2020-11-30 00:44

    Fluent assertions does deep comparisons of arrays actualArray.Should().BeEquivalentTo(expectedArray)

    0 讨论(0)
  • 2020-11-30 00:54

    this test compares a date input, checks if its a leap year, if so, outputs 20 leap years from the inputted date, if not, outputs the NEXT 20 leap years, myTest.Testing refers to the myTest instance which in turn calls the values from a List called Testing containing the calculated values required. part of an exercise I had to do.

    [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);
            }
    
    0 讨论(0)
  • 2020-11-30 00:57

    If you want to check that each contains the same collection of values then you should use:

    CollectionAssert.AreEquivalent(expected, actual);
    

    Edit:

    "Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object." - https://msdn.microsoft.com/en-us/library/ms243779.aspx

    0 讨论(0)
  • 2020-11-30 00:57
    List<AdminUser> adminDetailsExpected = new List<AdminUser>()
    {
    new AdminUser  {firstName = "test1" , lastName = "test1" , userId = 
    "001test1"  },
    new AdminUser {firstName = "test2" , lastName = "test2" , userId = 
    "002test2"   }
    };
    

    //Act

    List<AdminUser> adminDetailsActual = RetrieveAdmin(); // your retrieve logic goes here
    

    //Assert

    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
    
    0 讨论(0)
提交回复
热议问题