0

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

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

Create 方法返回一个自定义集合。

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

也传递给了 Create 方法。但我无法对自定义进行 1:1 的比较

收藏。我将不得不过滤、向下钻取、排序等......事情才能成功

断言...

我对单元测试很了解,但我对测试这样的场景一无所知,而没有将太多逻辑带入我真正想要避免的断言本身。

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

更新

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

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

断言和测试逻辑增加。

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

1 回答 1

0

在我看来,您需要将“创建”的代码分解成更小的部分。函数中有这么多参数通常表明将太多功能混合到一个对象中。

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

于 2013-03-05T19:48:40.683 回答