1. Joel Ross
  2. TrafficLight

Source

TrafficLight / src / Tests / Services / ApplicationServiceSpecs.cs

using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using RossCode.TrafficLight.Core.Domain;
using RossCode.TrafficLight.Core.Domain.Repositories;
using RossCode.TrafficLight.Core.Services;
using RossCode.TrafficLight.Core.Utilities;
using RossCode.TrafficLight.Tests.Utilties;

namespace RossCode.TrafficLight.Tests.Services
{
    public static class ApplicationServiceSpecs
    {
        public class when_initializing_the_application : under.the_default_context
        {
            protected override void When()
            {
                SUT.StartMonitoring();
            }

            [Test]
            public void it_should_retrieve_a_list_of_projects()
            {
                ProjectRepository.Verify(repo => repo.GetAll());
            }

            [Test]
            public void it_should_start_the_timer()
            {
                Timer.Verify(timer => timer.Start());
            }

            [Test]
            public void it_should_check_the_status_of_the_projects()
            {
                ProjectService.Verify(service => service.CheckAndSetStatusesFor(Projects));
            }
        }

        public class when_the_timer_elapses : under.the_default_context
        {
            protected override void EstablishContext()
            {
                base.EstablishContext();
                SUT.StartMonitoring();
            }

            protected override void When()
            {
                Timer.Raise(timer => timer.TimerElapsed += null, this, EventArgs.Empty);
            }

            [Test]
            public void it_should_check_the_status_of_the_projects()
            {
                ProjectService.Verify(service => service.CheckAndSetStatusesFor(Projects));
            }
        }

        public static class under
        {
            public abstract class the_default_context : ContextSpecification
            {
                protected Mock<IProjectRepository> ProjectRepository;
                protected Mock<IProjectService> ProjectService;
                protected Mock<ITimer> Timer;
                protected IList<Project> Projects;

                protected IProjectMonitorService SUT;

                protected override void EstablishContext()
                {
                    Projects = new List<Project>();

                    ProjectRepository = new Mock<IProjectRepository>();
                    ProjectRepository.Setup(repo => repo.GetAll()).Returns(Projects);

                    ProjectService = new Mock<IProjectService>();
                    Timer = new Mock<ITimer>();

                    SUT = new ProjectMonitorService(ProjectRepository.Object, ProjectService.Object, Timer.Object);
                }
            }
        }
    }
}