单元测试由大量逻辑创建的自定义集合

时间:2013-03-05 19:43:04

标签: unit-testing

我有一个带有Create方法的Factory类,该方法有8个参数。

所有这8个参数在Create方法中组合使用。

Create方法返回自定义集合。

作为结果的自定义集合需要以某种方式与其他集合进行比较

也已经传递给Create方法。但我无法对自定义

进行1:1的比较

集合。我必须过滤,深入挖掘,排序等等......才能获得成功

...断言

我对单元测试了如指掌,但我对于测试这样的场景毫无头绪,没有给Assertion本身带来太多逻辑,我实际上想要避免这些。

由于Create方法返回日历的“Days”集合,我无法进行单日的手动测试,我必须测试整个集合。边缘案例测试是不够的。

更新

我在这里放了一个样本,这是我最容易理解的一个样本......

我的其他单元测试(未完成)会更难,因为我的日期范围更大,意味着更多

断言和测试的逻辑增加。

[Test]
        public void CreatePeriods_3TimeTablesWithin7VisibleDays_21Periods()
        {
            // ARRANGE
            IDateService dateService = new DateService();
            LessonplannerFactory factory = new LessonplannerFactory(dateService);

            DateTime startDate = new DateTime(2013, 03, 01);
            DateTime endDate = new DateTime(2013, 03, 15);

            String schoolclassCode1 = "BIO1";
            String schoolclassCode2 = "BIO2";
            String schoolclassCode3 = "BIO3";

            IEnumerable<TimeTable> timetableA = new List<TimeTable> { 
                new TimeTable { LessonNumber = 3, SchoolclassCodeMonday = schoolclassCode3, WeekTypeState = TimeTable.WeekType.A },
                new TimeTable { LessonNumber = 2, SchoolclassCodeMonday = schoolclassCode2, WeekTypeState = TimeTable.WeekType.A }, 
                new TimeTable { LessonNumber = 1, SchoolclassCodeMonday = schoolclassCode1, WeekTypeState = TimeTable.WeekType.A }, 
            };

            TimeTable.WeekType selectedWeekType = TimeTable.WeekType.A;
            TimeTable.WeekType startWeekType = TimeTable.WeekType.A;
            DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
            IEnumerable<DayOfWeek> visibleWeekDays = new List<DayOfWeek> { DayOfWeek.Friday, DayOfWeek.Sunday, DayOfWeek.Monday };

            int numberOfVisibleDays = 7; int numberOfTimeTables = 3;

            // ACT
            var periods = factory.Create(startDate, endDate, 0, timetableA, null, selectedWeekType, startWeekType, firstDayOfWeek, visibleWeekDays);

            // sort by both properties to make this unit test testable...
            var sortedPeriods = periods.OrderBy(p => p.LessonNumber).ThenBy(p => p.LessonDate);

            // ASSERT

            Assert.IsTrue(periods.Count() == numberOfTimeTables * numberOfVisibleDays);

            // LessonNumber 1
            Assert.AreEqual(sortedPeriods.ElementAt(0).LessonNumber, 1);
            Assert.AreEqual(sortedPeriods.ElementAt(1).LessonNumber, 1);
            Assert.AreEqual(sortedPeriods.ElementAt(2).LessonNumber, 1);
            Assert.AreEqual(sortedPeriods.ElementAt(3).LessonNumber, 1);
            Assert.AreEqual(sortedPeriods.ElementAt(4).LessonNumber, 1);
            Assert.AreEqual(sortedPeriods.ElementAt(5).LessonNumber, 1);
            Assert.AreEqual(sortedPeriods.ElementAt(6).LessonNumber, 1);

            Assert.AreEqual(sortedPeriods.ElementAt(0).SchoolclassCode, string.Empty);
            Assert.AreEqual(sortedPeriods.ElementAt(1).SchoolclassCode, string.Empty);
            Assert.AreEqual(sortedPeriods.ElementAt(2).SchoolclassCode, schoolclassCode1);
            Assert.AreEqual(sortedPeriods.ElementAt(3).SchoolclassCode, string.Empty);
            Assert.AreEqual(sortedPeriods.ElementAt(4).SchoolclassCode, string.Empty);
            Assert.AreEqual(sortedPeriods.ElementAt(5).SchoolclassCode, schoolclassCode1);
            Assert.AreEqual(sortedPeriods.ElementAt(6).SchoolclassCode, string.Empty);

            Assert.AreEqual(sortedPeriods.ElementAt(0).LessonDate, new DateTime(2013, 03, 01));
            Assert.AreEqual(sortedPeriods.ElementAt(1).LessonDate, new DateTime(2013, 03, 03));
            Assert.AreEqual(sortedPeriods.ElementAt(2).LessonDate, new DateTime(2013, 03, 04));
            Assert.AreEqual(sortedPeriods.ElementAt(3).LessonDate, new DateTime(2013, 03, 08));
            Assert.AreEqual(sortedPeriods.ElementAt(4).LessonDate, new DateTime(2013, 03, 10));
            Assert.AreEqual(sortedPeriods.ElementAt(5).LessonDate, new DateTime(2013, 03, 11));
            Assert.AreEqual(sortedPeriods.ElementAt(6).LessonDate, new DateTime(2013, 03, 15));

            // LessonNumber 2
            // do the same as above

            // LessonNumber 3  
            // do the same as above
        }

1 个答案:

答案 0 :(得分:0)

在我看来,您需要将代码分解为“创建”为更小的部分。在函数中包含如此多的参数通常是将太多功能组合到一个对象中的标志。

如果您可以将工作划分为更小的对象,则可以更轻松地测试每个对象的功能,然后可以使用更高级别的测试来确保您正确地将工作委派给所有较小的对象。