猿问

模拟 WindsorContainer 以测试 ComponentRegistration

我正在使用 Castle.Windsor 4.1.1 并且我有这样的注册:

container.Register(Component.For<IMessageMappingManager>().ImplementedBy<MessageMappingManager>());

现在我想测试注册是否正常,因此我使用 Moq 4.10.0 模拟了一个 _container:

_container = new Mock<IWindsorContainer>();

现在我想像这样测试注册:

_container.Verify(f => f.Register(Component.For<IMessageMappingManager>().ImplementedBy<MessageMappingManager>()), Times.Once);

或者像这样:

_container.Verify(f=>f.Register(It.IsAny<ComponentRegistration<IMessageMappingManager>().ImplementedBy<MessageMappingManager>()>()), Times.Once);

但它们都不起作用。
有人可以帮忙吗?

提前致谢。


MYYA
浏览 143回答 1
1回答

喵喵时光机

您答案中的单元测试不会测试任何内容。第一个问题是您正在模拟您的被测系统。整个点来测试您对被测系统的实现。然后您唯一的测试是对模拟对象的调用发生了。验证是否发生了实际注册很容易。此示例使用安装程序,因为我使用它们来清理大量注册代码。public class EmailInstaller : IWindsorInstaller{&nbsp; &nbsp; public void Install(IWindsorContainer container, IConfigurationStore store)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; container.Register(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Component.For(typeof(IResolveApplicationPath))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ImplementedBy(typeof(ApplicationPathResolver))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .LifeStyle.PerWebRequest);&nbsp; &nbsp; &nbsp; &nbsp; container&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Register(Component.For(typeof(IGenerateEmailMessage)).ImplementedBy(typeof(EmailMessageGenerator))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .LifeStyle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .PerWebRequest);&nbsp; &nbsp; &nbsp; &nbsp; container&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Register(Component.For(typeof(ISendEmail)).ImplementedBy(typeof(EmailSender))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .LifeStyle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .PerWebRequest);&nbsp; &nbsp; &nbsp; &nbsp; container.Register(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Component.For<NotificationConfigurationSection>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UsingFactoryMethod(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel.Resolve<IConfigurationManager>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetSection<NotificationConfigurationSection>("notificationSettings")));&nbsp; &nbsp; }}然后我的测试看起来像&nbsp; &nbsp; public class WhenInstallingEmailComponents : SpecificationBase&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private IWindsorContainer _sut;&nbsp; &nbsp; &nbsp; &nbsp; protected override void Given()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _sut = new WindsorContainer();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected override void When()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _sut.Install(new EmailInstaller());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; [Then]&nbsp; &nbsp; &nbsp; &nbsp; public void ShouldConfigureEmailSender()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var handler = _sut&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetHandlersFor(typeof(ISendEmail))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Single(imp => imp.ComponentModel.Implementation == typeof(EmailSender));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.That(handler, Is.Not.Null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; [Then]&nbsp; &nbsp; &nbsp; &nbsp; public void ShouldConfigureEmailGenerator()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var handler = _sut&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetHandlersFor(typeof(IGenerateEmailMessage))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Single(imp => imp.ComponentModel.Implementation == typeof(EmailMessageGenerator));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.That(handler, Is.Not.Null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}这是 GetHandlersFor 扩展方法public static class WindsorTestingExtensions{&nbsp; &nbsp; public static IHandler[] GetAllHandlers(this IWindsorContainer container)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return container.GetHandlersFor(typeof(object));&nbsp; &nbsp; }&nbsp; &nbsp; public static IHandler[] GetHandlersFor(this IWindsorContainer container, Type type)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return container.Kernel.GetAssignableHandlers(type);&nbsp; &nbsp; }&nbsp; &nbsp; public static Type[] GetImplementationTypesFor(this IWindsorContainer container, Type type)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return container.GetHandlersFor(type)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(h => h.ComponentModel.Implementation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(t => t.Name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToArray();&nbsp; &nbsp; }}我使用基类 SpecficicationBase 使我的单元测试读起来像 BDD 样式测试,但您应该能够理解发生了什么。实例化你的容器调用安装程序来注册您的组件。检查容器的接口实现类型。这是 Castle 项目中关于如何测试注册码的一个很好的链接。
随时随地看视频慕课网APP
我要回答