A place to keep my thoughts on programming

August 24, 2009 .net , , ,

Setting up mocks for an inner autofac container

This is definitely an edge case testing scenario, so i don’t know how useful this utility class is in general, but i thought it was kinda fun deferred execution stuff, so why not post it?

Here’s my scenario. I’ve built some Dream REST services that i’ve set up to create an inner Autofac container per request to create per request instances of objects — things like NHibernate ISession and other disposable resources that only make sense in a per request context.

Now i’m writing my unit tests around these services, and need to provide mocks for these inner container created objects. I should also mention that to test the services, i am firing up a DreamHost, since the services can only be tested in the context of the full pipeline. Yeah, i know, smells a bit functional, but that’s what I have to work with right now. And i need these objects to be ContainerScoped (i.e. per inner container singletons), so that multiple Resolve‘s return the same instance, but still return different instances on multiple requests. Ok, ok, i know the tests are doing too much… Like i said, this is an edge case. It’s not strictly a unit test, but i still want coverage on this code. Getting around this would require refactoring of code that’s not part of my project, so there you go.

What I want to do is set up the mock for the inner container instance on creation, which doesn’t happen until i’ve handed over execution control to the Act part of the test. This lead me to create a factory that provides a hook for setting up the mock on creation of the mock:

public class DelegatedMoqFactory<T> where T : class
 private Action<Mock<T>, IContext> setupCallback;
 private Mock<T> mock;

 public Mock<T> CurrentMock { get { return mock; } }

 public T CreateInstance(IContext container)
  mock = new Mock<T>();
  if (setupCallback != null)
   setupCallback(mock, container);
  return mock.Object;

 public void OnResolve(Action<Mock<T>, IContext> setupCallback)
  this.setupCallback = setupCallback;

A sample autofac wire-up looks like this:

builder.Register(c => c.Resolve<DelegatedMoqFactory<IAuthService>>().CreateInstance(c))

With a test setup of the IAuthService being done like this:

    .OnResolve(m => m.Setup(x =>

The open generic of DelegateMoqFactory is registered with default scope, since i want it to exist outside the inner scope, so that i can resolve it to wire up my expectations for the mock. Then on the first access for IAuthService inside the inner scope, the DelegateMoqFactory creates the mock and calls my OnResolve callback to set up the mock.

The reason there is also a CurrentMock accessor is so that I can do verification on the mock after the inner container has gone out of scope, like this:

    .CurrentMock.Verify(x =>
        x.CreateAuthToken(It.IsAny<IAccount>()), Times.Never());

This class should be useful whenever you are testing some code that internally creates an inner container and scoping the objects usually created under ContainerScope as default scope doesn’t work (likely because there’s multiple inner containers). We still get per inner container instances, but get to wire them up with deferred setups that don’t come into play until the mocks are actually pulled from the inner container.

Leave a comment