我在使用Autofac密钥注册的工厂编写测试时遇到麻烦.
I'm having trouble writing tests around a factory that uses Autofac keyed registrations.
在Autofac模块中,我注册如下内容:
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>();然后,工厂的构造函数会通过Autofac将一个不错的索引注入其构造函数:
Then the constructor for the factory gets a nice Index injected into its constructor by Autofac:
public MessageHandlerFactory( IIndex<MessageTypeEnum, IMessageHandler> messageTypeToHandlerMap)但是,当我使用 automock.Create< MessageHandlerFactory>(code)时,我无法弄清楚如何为Automock的单元测试注入 IIndex< >.告诉AutoMock提供消息处理程序实现并不会使它们进入键控索引.创建IIndex的显式实现并要求Automock提供该方法也不起作用-在这两种情况下,我的工厂都会注入一个空的 IIndex<,> .
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?
推荐答案我发现AutoMock并没有内置对键控组件的支持,但是确实有重载的工厂方法,可用于配置容器.
I found the AutoMock doesn't really have built in support for keyed components, but does has overloads of the factory methods that let you configure the container.
这可行:
var messageAMock = new Mock<IMessageHandler>(); var autoMock = AutoMock.GetStrict(builder => builder.RegisterInstance(messageAMock.Object).Keyed<IMessageHandler>(MessageTypeEnum.A));更多推荐
AutoMock
发布评论