Unit testing your IoC container

I decided to write this post after reading a question on StackOverflow, where the OP was asking for recommendations to simplify his IoC setup code – to make it more readable and also to prevent runtime errors when resolving instances, whenever a change is made.

I feel his pain. Anyone that used and configured an IoC container such as Unity knows how easy it is to break things when you add or change an existing configuration. So, how to detect if things are broken until it’s too late (runtime errors)? Writing tests, obviously.

In this post I’ll show you a quick and simple way to test your dependencies using Unity IoC container.


The scenario – a shopping cart service

Let’s assume a simple scenario – a service that allows users to check the all the items and corresponding prices of his shopping cart. The service uses a logger, a repository to read the information of the cart and a currency service that is used to display the prices in the currency selected by the user:

Source code is the following (methods omitted for brevity):

public interface ILogger
{
}

public class AsyncLogger : ILogger
{
}

public class ConsoleLogger : ILogger
{
}

public class FileLogger : ILogger
{
}

public interface ICurrencyApiClient
{
}

public class CurrencyApiClient : ICurrencyApiClient
{
    private readonly string _apiKey;
    private readonly ILogger _logger;

    public CurrencyApiClient(string apiKey, ILogger logger)
    {
        _apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }
}

public interface IRepository
{
}

public class Repository : IRepository
{
    private readonly string _connectionString;

    public Repository(string connectionString)
    {
        _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
    }
}

public class ShoppingCartService : IShoppingCartService
{
    private readonly IRepository _repository;
    private readonly ICurrencyApiClient _currencyApiClient;
    private readonly ILogger _logger;

    public ShoppingCartService(IRepository repository, ICurrencyApiClient currencyApiClient, ILogger logger)
    {
        _repository = repository ?? throw new ArgumentNullException(nameof(repository));
        _currencyApiClient = currencyApiClient ?? throw new ArgumentNullException(nameof(currencyApiClient));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }
}

Configuring the dependencies

The first thing to do is to put the IoC code in a class library that can be easily referenced by a test project. You can then add your Bootstrapper class, something like this:

    public class Bootstrapper
    {
        public IUnityContainer Init()
        {
            var container = new UnityContainer();

            // dependencies registration goes here....

            return container;
        }
    }

Please note that the Init() method returns the IoC container – you’ll need it in the unit tests when trying to resolve the instances.

Full source code of the Bootstrapper is the following:

    public class Bootstrapper
    {
        private readonly NameValueCollection _appSettings;
        private readonly ConnectionStringSettingsCollection _connectionStrings;

        public Bootstrapper(
            NameValueCollection appSettings = null,
            ConnectionStringSettingsCollection connectionStrings = null
        )
        {
            _appSettings = appSettings ?? ConfigurationManager.AppSettings;
            _connectionStrings = connectionStrings ?? ConfigurationManager.ConnectionStrings;
        }

        public IUnityContainer Init()
        {
            var container = new UnityContainer();

            // default logger is AsyncLogger
            container.RegisterType();

            // named instances for loggers
            container.RegisterType(nameof(AsyncLogger));
            container.RegisterType(nameof(ConsoleLogger));
            container.RegisterType(nameof(FileLogger));

            container.RegisterType(new InjectionFactory(CreateCurrencyApiClient));
            container.RegisterType(new InjectionFactory(CreateRepository));

            container.RegisterType();

            return container;
        }
    }

Some notes:

  • The constructor takes 2 optional parameters (appSettings and connectionStrings), which can be used for testing purposes. If no values are provided it will use the values from the configuration file.
  • AsyncLogger, ConsoleLogger and FileLogger are registered as named instances
  • ICurrencyApiClient and IRepository are registered using a factory method

Configuring the unit tests

Now it’s time to write the unit tests. Instead of manually adding tests for every single dependency, we can use the Registrations property of IUnityContainer to get the metadata of all registered dependencies:

Writing the tests (using NUnit and Fluent Assertions):

 
    [TestFixture]
    public class BootstrapperTests
    {
        private static IUnityContainer Container => new Bootstrapper().Init();

        private static IEnumerable UnityRegistrations
        {
            get
            {
                var registrations = Container.Registrations
                                             .Where(x => x.RegisteredType != typeof(IUnityContainer))
                                             .Select(x => new TestCaseData(x.RegisteredType, x.Name));

                return registrations;
            }
        }

        [Test]
        [TestCaseSource(nameof(UnityRegistrations))]
        public void GivenATypeAndName_WhenResolvingInstance_InstanceShouldNotBeNull(Type registeredType, string instanceName)
        {
            // arrange/act
            object instance = Container.Resolve(registeredType, instanceName);

            // assert
            using (new AssertionScope())
            {
                instance.Should().BeAssignableTo(registeredType);
                instance.Should().NotBeNull();
            }
        }
    }

It’s just as simple as that. Given the registered type and the instance name of the dependencies, I can try to resolve them. If the instance is null or an exception is thrown the test will fail, which means that there is something wrong with our Bootstrapper. Also, I check if the returned instance has the expected type.

Running the code using Reshaper:

Some tests failed because I forgot to add a configuration file with the settings used by both the CurrencyApiClient and Repository. Test for IShoppingCartService fails as well because it uses both dependencies.

Fixing the code and running the tests:

All good now. As you can see, there is a test for every single type/instance name.

Final thoughts

You should add as many unit tests as possible to your code – IoC setup is no exception. Also, these tests do not exclude the usage of other type of tests such as integration or smoke tests.

My article is a good starting point, but this might not be enough. For example, if you have ASP.NET MVC or ASP.NET Web API applications, you should test your DependencyResolver in order to ensure that all controllers are being instantiated correctly (i.e. without throwing exceptions).

Consider also running these tests for every single environment – each environment has its own configuration, so better be safe than sorry 😉

Happy coding!

Advertisements

Implementing a basic IoC container using C#

Implementing a basic IoC container using C#, step by step.

Table of contents

  • References
  • Downloads
  • Continue reading

    ASP.NET Web Services Dependency Injection using Unity

    Recently, I had to setup Unity IoC container in an existing ASP.NET 3.5 Web Forms application. This application had not only web pages (.aspx files), but also some Web Services (.asmx files). After some research, I found out David Hayden’s screencast: Unity IoC and ASP.NET screencast – Dependency Injection into Web Pages.

    You can use a similar technique for your Web Services – that’s what I’ll show you in this article.

    Table of contents

    Example – Adding logging to your application

    You have the following interface and its implementation:

    public interface ILogger
    {
        void Write(string message);
    }
    
    public class DebugLogger : ILogger
    {
        public void Write(string message)
        {
            Debug.WriteLine(message);
        }
    }
    

     

    Step 1: Setting up the container in Global.asax

    The first step is to setup Unity Container in Global.asax file. This is a good place to do it because it can be accessed either by web pages or by web services.
    The CreateContainer() method is the place where the dependencies are specified.

    public class Global : HttpApplication, IContainerAccessor
    {
        private static IUnityContainer _container;
    
        public static IUnityContainer Container
        {
            get
            {
                return _container;
            }
            private set
            {
                _container = value;
            }
        }
    
        IUnityContainer IContainerAccessor.Container
        {
            get
            {
                return Container;
            }
        }
    
        protected void Application_Start(object sender, EventArgs e)
        {
            CreateContainer();
        }
    
        protected virtual void CreateContainer()
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterType<ILogger, DebugLogger>();
            
            Container = container;
        }
    }
    

     

    Step 2: Creating a base class for the services

    Create a generic BaseService that all your services will inherit from. The dependencies will be injected when you create an instance of the service (default constructor).

    public abstract class BaseService<T> : System.Web.Services.WebService where T : class
    {
        public BaseService()
        {
            InjectDependencies();
        }
    
        protected virtual void InjectDependencies()
        {
            HttpContext context = HttpContext.Current;
    
            if (context == null)
                return;
    
            IContainerAccessor accessor = context.ApplicationInstance as IContainerAccessor;
    
            if (accessor == null)
                return;
    
            IUnityContainer container = accessor.Container;
    
            if (container == null)
                throw new InvalidOperationException("Container on Global Application Class is Null. Cannot perform BuildUp.");
    
            container.BuildUp(this as T);
        }
    }
    

     

    Step 3: Setting up the services

    Now all you need to do is to inherit from the BaseService and invoke its base constructor.
    Don’t forget to add the [Dependency] attribute to your dependency, and it has to be public.

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class DummyService : BaseService<DummyService>
    {
        [Dependency]
        public ILogger Logger
        {
            get;
            set;
        }
    
        public DummyService() : base()
        {
        }
    
        [WebMethod]
        public string HelloWorld(string name)
        {
            string message = string.Format("Hello World, {0}!", name);
    
            this.Logger.Write(message);
    
            return message;
        }
    }
    

    That’s it! Now you just need to compile and run the application and see it in action 🙂

    Feel free to download the demo application

    References

    Downloads

    Download the demo project (VS2010): UnityAsmxWebServices.zip