AutoMock

编程入门 行业动态 更新时间:2024-10-22 18:34:51
本文介绍了AutoMock-如何使用键注册进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用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

本文发布于:2023-11-14 19:25:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AutoMock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!