为什么这些相同的结果不能通过AreEqual和AreSame测试断言?

时间:2016-02-25 23:10:04

标签: c# mstest equality generic-list assertions

我有这个测试代码:

[TestMethod]
public void TestGetDatesForNthDOWOfMonth()
{
    List<DateTime> firstFridaysInFirstQuarterOf2017 = new List<DateTime>();
    // These were determined by looking at a calendar
    firstFridaysInFirstQuarterOf2017.Add(new DateTime(2017, 1, 6));
    firstFridaysInFirstQuarterOf2017.Add(new DateTime(2017, 2, 3));
    firstFridaysInFirstQuarterOf2017.Add(new DateTime(2017, 3, 3));
    DayOfWeek dow = DayOfWeek.Friday;
    DateTime startDate = new DateTime(2017, 1, 1);
    DateTime endDate = new DateTime(2017, 3, 31);
    List<DateTime> testFirstFridaysInFirstQuarterOf2017 = RoboReporterConstsAndUtils.GetDatesForNthDOWOfMonth(startDate, endDate, dow, 1);
    //Assert.AreEqual(firstFridaysInFirstQuarterOf2017, testFirstFridaysInFirstQuarterOf2017);
    Assert.AreSame(firstFridaysInFirstQuarterOf2017, testFirstFridaysInFirstQuarterOf2017);
}

测试方法是:

internal static List<DateTime> GetDatesForNthDOWOfMonth(
    DateTime startDate,
    DateTime endDate,
    DayOfWeek dayOfWeek,
    int ordinal)
{
    var foundDates = new List<DateTime>();
    // Make sure ordinal parameter is within the allowable range
    //If it's too high, set it to 5 (last week) so it returns the last
    // qualifying day of each month
    ordinal = Math.Min(Math.Max(ordinal, 1), 5);
    while (startDate < endDate)
    {
        int month = startDate.Month;
        //Set workingDate to the first day of the month.
        var workingDate = new DateTime(startDate.Year, month, 1);
        //Set workingDate to the first occurrence of the DayOfWeek parameter
        workingDate = (int)workingDate.DayOfWeek > (int)dayOfWeek
            ? workingDate.AddDays(7 - (int)workingDate.DayOfWeek + (int)dayOfWeek)
            : workingDate.AddDays((int)dayOfWeek - (int)workingDate.DayOfWeek);

        //Advance the date by the number of days required to satisfy the ordinal parameter
        workingDate = workingDate.AddDays((ordinal - 1) * 7);
        //If the date has crossed the month boundary, step back a week.
        //So the last occurrence of the target day is returned
        workingDate = workingDate.Month != month ? workingDate.AddDays(-7) : workingDate;
        foundDates.Add(workingDate);
        startDate = startDate.AddMonths(1);
    }
    return foundDates;
}

两个DateTime列表包含相同的确切值,一直到HHMMSS。那么为什么断言会失败呢? Assert.AreEqual()失败,Assert.AreSame()也失败了!是的,它们不是完全相同的对象,但它们的值是相同的,为什么这种不确定性呢?

更新

我尝试了Jon Skeet的想法here

. . .
var firstNotSecond = mockedMonthsAndTruncatedYears.Except(monthsAndTruncatedYears).ToList();
var secondNotFirst = monthsAndTruncatedYears.Except(mockedMonthsAndTruncatedYears).ToList();
Assert.IsTrue((null == firstNotSecond) && (null == secondNotFirst));

...但是得到,“'System.Collections.Generic.List'不包含'Except'的定义,也没有扩展方法'除'接受第一个参数可以找到类型'System.Collections.Generic.List'(您是否缺少using指令或程序集引用?)

2 个答案:

答案 0 :(得分:1)

List是一种引用类型,因此当您比较两个列表时,比较引用,而不是值。

因此,为了比较列表项,您应该检查两个列表中的每个项目。

请看这里:Compare two List<T> objects for equality, ignoring order

或者在这里:Quickest way to compare two List<>

答案 1 :(得分:0)

这就是它所需要的:

        [TestMethod]
        public void TestGetDatesForNthDOWOfMonth()
        {
            List<DateTime> firstFridaysInFirstQuarterOf2017 = new List<DateTime>
    ();
            // These were determined by looking at a calendar
            firstFridaysInFirstQuarterOf2017.Add(new DateTime(2017, 1, 6));
            firstFridaysInFirstQuarterOf2017.Add(new DateTime(2017, 2, 3));
            firstFridaysInFirstQuarterOf2017.Add(new DateTime(2017, 3, 3));
            DayOfWeek dow = DayOfWeek.Friday;
            DateTime startDate = new DateTime(2017, 1, 1);
            DateTime endDate = new DateTime(2017, 3, 31);
            List<DateTime> testFirstFridaysInFirstQuarterOf2017 = 
    RoboReporterConstsAndUtils.GetDatesForNthDOWOfMonth(startDate, endDate, dow, 1);
            var firstNotSecond = 
 firstFridaysInFirstQuarterOf2017.Except(testFirstFridaysInFirstQuarterOf2017).ToList();
            var secondNotFirst = 
    testFirstFridaysInFirstQuarterOf2017.Except(firstFridaysInFirstQuarterOf2017).To
    List();
            Assert.IsTrue((firstNotSecond.Count == 0) && (secondNotFirst.Count 
    == 0));
        }

有趣的是,这些项目不被视为相同/相等,除非使用了除外。