﻿using System;
using Microsoft.Practices.ServiceLocation;
using Moq;
using NUnit.Framework;
using Uncas.BuildPipeline.Commands;
using Uncas.BuildPipeline.Repositories;
using Uncas.BuildPipeline.Utilities;

namespace Uncas.BuildPipeline.Tests.Unit
{
    [TestFixture]
    public class CommandBusTests
    {
        [Test]
        public void Publish_HandlerNotRegisterd_ThrowsInvalidOperationException()
        {
            var mock = new Mock<IServiceLocator>();
            var bus = new CommandBus(mock.Object);

            Assert.Throws<InvalidOperationException>(() => bus.Publish(new UpdateGitMirrors()));
        }

        [Test]
        public void Publish_HandlerRegistered_GetsResolvedOnce()
        {
            var mock = new Mock<IServiceLocator>();
            mock.Setup(x => x.GetInstance(typeof (UpdateGitMirrorsHandler))).Returns(
                new UpdateGitMirrorsHandler(new GitUtility(), new ProjectReadStore()));
            var bus = new CommandBus(mock.Object);

            bus.Publish(new UpdateGitMirrors());

            mock.Verify(x => x.GetInstance(typeof (UpdateGitMirrorsHandler)), Times.Once());
        }
    }
}
﻿using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Moq;
using NUnit.Framework;
using Ploeh.AutoFixture;
using Uncas.BuildPipeline.Commands;

namespace Uncas.BuildPipeline.Tests.Unit
{
    [TestFixture]
    public class CommandBusTests : WithFixture<CommandBus>
    {
        [Test]
        public void Publish_HandlerNotRegisterd_ThrowsInvalidOperationException()
        {
            Fixture.Register<IServiceLocator>(() => new UnityServiceLocator(new UnityContainer()));

            Assert.Throws<ActivationException>(() => Sut.Publish(Fixture.Create<UpdateGitMirrors>()));
        }

        [Test]
        public void Publish_HandlerRegistered_GetsResolvedOnce()
        {
            var mock = new Mock<IServiceLocator>();
            mock.Setup(x => x.GetInstance(typeof (UpdateGitMirrorsHandler))).Returns(
                Fixture.Create<UpdateGitMirrorsHandler>());
            Fixture.Register(() => mock.Object);

            Sut.Publish(Fixture.Create<UpdateGitMirrors>());

            mock.Verify(x => x.GetInstance(typeof (UpdateGitMirrorsHandler)), Times.Once());
        }
    }
}
Use autofixture for unit tests of the command bus.
Use autofixture for unit tests of the command bus.
