1
votes

I have a convention for StructureMap that looks like this:

public class FakeRepositoriesConvention : IRegistrationConvention
{
    public void Process(Type type, global::StructureMap.Configuration.DSL.Registry registry)
   {
        if (type.Name.StartsWith("Fake") && type.Name.EndsWith("Repository"))
        {
            string interfaceName = "I" + type.Name.Replace("Fake", String.Empty);
            registry.AddType(type.GetInterface(interfaceName), type);
        }
    }
}

I want to implement unit tests for this, but I don't know how to do that.

My first thought was to send in a mocked Registry and just test that AddType() is called with the right parameters. I can't get that to work though, probably because AddType() is not virtual. Registry implements IRegistry but that doesn't help me since the Process method doesn't accept an interface.

So my question is - how can I test this?

(I'm using nUnit and RhinoMocks)

1

1 Answers

3
votes

You can skip mocking altogether and use simplified version of your registry and component with predefined dummy types:

// Dummy types for test usage only
public interface ICorrectRepository { }
public class FakeCorrectRepository : ICorrectRepository { }

[Test]
Process_RegistersFakeRepositoryType_ThroughInterfaceTypeName()
{
    var registry = new Registry();
    var convention = new FakeRepositoriesConvention();

    // exercise test
    convention.Process(typeof(FakeCorrectRepository), registry);

    // assert it worked
    var container = new Container(c => c.AddRegistry(registry));
    var instance = container.GetInstance<ICorrectRepository>();
    Assert.That(instance, Is.Not.Null);
}

If your convention works as you assume it does, test above should pass.