I'm having trouble writing tests around a factory that uses Autofac keyed registrations.
In an Autofac module, I register things like this:
builder.RegisterType<TypeAMessageHandler>().As<IMessageHandler>()
.Keyed<IMessageHandler>(MessageTypeEnum.A);
builder.RegisterType<TypeBMessageHandler>().As<IMessageHandler>()
.Keyed<IMessageHandler>(MessageTypeEnum.B);
builder.RegisterType<MessageHandlerFactory().As<IMessageHandlerFactory>();
Then the constructor for the factory gets a nice Index injected into its constructor by Autofac:
public MessageHandlerFactory(
IIndex<MessageTypeEnum, IMessageHandler> messageTypeToHandlerMap)
However, I can't figure out how to inject an IIndex<,>
for unit testing with Automock, when I use automock.Create<MessageHandlerFactory>()
. Telling the AutoMock to Provide message handler implementations doesn't get them into the keyed index. Creating an explicit implementation of IIndex and asking Automock to Provide that also doesn't work - in both cases, my factory gets an empty IIndex<,>
injected.
What is the right way to test keyed registrations?