Writing IoC Supported Integration Tests using AutoFac
2 min read
2 min read
Using a dependency injection framework can greatly facilitate your code's testability in that you don't have any "glue" code for managing a classes' dependency that needs to be mocked (if even possible) when writing unit tests. But what about when writing integration tests? In such case you'd probably want to use your IoC container's configuration for resolving types in order to also verify the proper integration of your components, frankly your dependency injection configuration, right?
With AutoFac it is actually quite simple. What your integration test needs to do is to himself create theContainerBuilder()
and accordingly register the dependencies. What you should do is the following. Assume you have a Visual Studio project
that encapsulates your business logic called "BusinessLogic". Then the best strategy is to create a so-called
Autofac module per project (and hence dll) to configure the dependencies within that specific project. This is
done by extending the abstract class Autofac.Module
likepublic class BusinessLogicModule : Module
{
protected override void Load(ContainerBuilder builder)
{
//register other modules/dependencies here
}
}
BusinessLogic.IntegrationTests
project, adding the project under test as a reference.using Autofac;A specific test case might then look as follows:
using Autofac.Core;
namespace BusinessLogic.IntegrationTest.Utils
{
public class IoCSupportedTest<TModule> where TModule : IModule, new()
{
private IContainer container;
public IoCSupportedTest()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new TModule());
container = builder.Build();
}
protected TEntity Resolve<TEntity>()
{
return container.Resolve<TEntity>();
}
protected void ShutdownIoC()
{
container.Dispose();
}
}
}
[TestClass]
public class UserRepositoryTest : IoCSupportedTest<BusinessLogicModule>
{
private IUserRepository userRepo;
[TestInitialize]
public void Setup()
{
this.userRepo = Resolve<IUserRepository>();
}
[TestCleanup]
public void TearDown()
{
userRepo = null;
ShutdownIoC();
}
//the tests
}