【问题标题】:Castle.MicroKernel.ComponentNotFoundException - When Unit TestingCastle.MicroKernel.ComponentNotFoundException - 单元测试时
【发布时间】:2016-02-23 16:40:35
【问题描述】:

我正在尝试对 Orchestrator 进行单元测试。

//Arrange
var containter = new WindsorContainer();
var Orch = containter.Resolve<ApiOrchestrator>();// Exception Thrown here

Orchestrator 的构造函数是:

public ApiOrchestrator(IApiWrap[] apiWraps)
{
    _apiWraps = apiWraps;
}

注册是

public class IocContainer : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<FrmDataEntry>().LifestyleTransient());
        container.Register(Component.For<ApiOrchestrator>().LifestyleTransient());
        container.Register(Component.For<IApiWrap>().ImplementedBy<ClassA>().LifestyleTransient());
        container.Register(Component.For<IApiWrap>().ImplementedBy<ClassB>().LifestyleTransient());
    }
}

IocContainer 在正在测试的项目中,但命名空间被引用,我可以new 使用 Orchestrator。我希望它只给我所有注册的 IApiWrap 的数组。

刚接触 Castle,我不明白缺少什么。代码修复会很好,但我真的很想知道为什么容器似乎没有注册协调器。

【问题讨论】:

  • 所以我认为我错过了从容器中调用 install 方法,但我不确定将什么用作 IWindsorInstaller

标签: inversion-of-control castle-windsor ioc-container castle


【解决方案1】:

好的,所以缺少 3 件事

  1. 对 Castle.Windsor.Installer 的引用
  2. 从容器调用安装程序以“查找”所有已注册的类。
  3. 安装程序还需要添加一个子解析器来制作类的集合,因为未注册特定集合并且编排器需要 IApiWrap 集合。

安装程序更改

public class IocContainer : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        //New Line
        container.Kernel.Resolver.AddSubResolver(
                  new CollectionResolver(container.Kernel, true));

        container.Register(Component.For<FrmDataEntry>().LifestyleTransient());
        container.Register(Component.For<ApiOrchestrator>().LifestyleTransient());
        container.Register(Component.For<IApiWrap>().ImplementedBy<SettledCurveImportCommodityPriceWrap>().LifestyleTransient());
        container.Register(Component.For<IApiWrap>().ImplementedBy<ForwardCurveImportBalmoPriceWrap>().LifestyleTransient());
    }
}

测试/解决变化

//Arrange
        var container = new WindsorContainer();

        //New Line
        container.Install(FromAssembly.InDirectory(new AssemblyFilter("","EkaA*") ));

        var Orch = container.Resolve<ApiOrchestrator>();

现在它可以工作了,但欢迎对代码所做的任何进一步解释或更正。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多