コード例 #1
0
        public void GetAllEventsByMonth_ValidResult_ReturnsAllEventsFromMonth()
        {
            DateTime now = DateTime.Today;

            /*  Return values for mock EventRepository */
            List<Event> mockEvents = new List<Event>
            {
                new Event()
                {
                    EventID = 1,
                    EventType = 1,
                    EventTitle = "Test Event 1",
                    EventDesc = "Meeting with Foo",
                    EventStart = now,
                    EventEnd = now.AddHours(1),
                    RepeatType = 2
                },
                new Event()
                {
                    EventID = 2,
                    EventType = 2,
                    EventTitle = "Test Event 2",
                    EventDesc = "Lunch with Bar",
                    EventStart = now.AddDays(1),
                    EventEnd = now.AddDays(1).AddHours(1),
                    RepeatType = 1
                },
                new Event()
                {
                    EventID = 3,
                    EventType = 1,
                    EventTitle = "Test Event 3",
                    EventDesc = "Date with Foo Bar",
                    EventStart = now.AddDays(-1),
                    EventEnd = now.AddDays(-1).AddHours(1),
                    RepeatType = 2
                },
                new Event()
                {
                    EventID = 4,
                    EventType = 2,
                    EventTitle = "Test Event 4",
                    EventDesc = "Client Update Meeting",
                    EventStart = now.AddMonths(1),
                    EventEnd = now.AddMonths(1).AddHours(1),
                    RepeatType = 3
                },
                new Event()
                {
                    EventID = 5,
                    EventType = 1,
                    EventTitle = "Test Event 5",
                    EventDesc = "Family Vacation",
                    EventStart = now.AddMonths(1),
                    EventEnd = now.AddMonths(1).AddHours(2),
                    RepeatType = 4
                }
            };

            /*  Initialize Mocks */
            var operationResult = new Mock<IOperationResult>();
            var eventRepository = new Mock<IEventRepository>();

            /*  Setup Mocks */
            eventRepository
                .Setup(x => x.GetAllEventsFromMonth(now.Year, now.Month))
                .Returns(mockEvents.Where(x => x.EventStart.Month == now.Month));

            eventRepository
                .Setup(x => x.Result.IsError)
                .Returns(false);

            /*  Initialize Service */
            EventService service = new EventService(
                operationResult.Object,
                eventRepository.Object
            );

            /*  Test Method */
            List<Event> results = service.GetAllEventsFromMonth(now.Year, now.Month);

            /*  Test to see returned events and mock events have the same number of elements */
            Assert.AreEqual(3, results.Count);

            /*  Test to see returned events are the same as mock events */
            foreach (Event ev in results)
            {
                Assert.AreEqual(now.Month, ev.EventStart.Month);
            }
        }