Source

TrafficLight / src / Tests / Services / ProjectServiceSpecs.cs

Full commit
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using RossCode.TrafficLight.Core.Eventing;
using RossCode.TrafficLight.Core.Eventing.Events;
using RossCode.TrafficLight.Core.Services;
using RossCode.TrafficLight.Tests.Utilties;
using RossCode.TrafficLight.Core.Domain;

namespace RossCode.TrafficLight.Tests.Services
{
    public static class ProjectServiceSpecs
    {
        public class when_getting_the_status_of_a_building_project : under.the_default_context
        {
            protected override void When()
            {
                SUT.CheckAndSetStatusesFor(Projects);
            }

            [Test]
            public void it_should_mark_the_project_as_building()
            {
                Assert.That(Project.CurrentStatus, Is.EqualTo(BuildStatus.Building));
            }

            [Test]
            public void it_should_call_the_callback()
            {
                Assert.That(CallbackCalled, Is.EqualTo(1));
            }
        }

        public class when_retrieving_the_same_project_status_twice : under.the_default_context
        {
            protected override void When()
            {
                SUT.CheckAndSetStatusesFor(Projects);
                SUT.CheckAndSetStatusesFor(Projects);
            }

            [Test]
            public void it_should_mark_the_project_as_building()
            {
                Assert.That(Project.CurrentStatus, Is.EqualTo(BuildStatus.Building));
            }

            [Test]
            public void it_should_call_the_callback_only_once()
            {
                Assert.That(CallbackCalled, Is.EqualTo(1));
            }
        }

        public static class under
        {
            public abstract class the_default_context : ContextSpecification
            {
                protected ProjectService SUT;
                protected IList<Project> Projects;
                protected Project Project;
                protected Mock<IProjectStatusResolverService> ProjectStatusResolver;
                protected int CallbackCalled;

                protected override void EstablishContext()
                {
                    Project = new Project();
                    Projects = new List<Project>{ Project };
                    
                    ProjectStatusResolver = new Mock<IProjectStatusResolverService>();
                    ProjectStatusResolver.Setup(resolver => resolver.CheckStatus(Project)).Returns(BuildStatus.Building);

                    DomainEvents.Register<BuildStatusChanged>(BuildStatusChanged);

                    SUT = new ProjectService(ProjectStatusResolver.Object);
                }

                private void BuildStatusChanged(BuildStatusChanged obj)
                {
                    CallbackCalled++;
                }
            }
        }
    }
}