【问题标题】:Nancy test fails because of missing modulePath for FakeNancyModule由于 FakeNancyModule 缺少 modulePath,Nancy 测试失败
【发布时间】:2011-12-29 17:03:54
【问题描述】:

当我尝试测试 Nancy 模块时,我得到以下信息:

StructureMap 异常代码:205 缺少请求的实例属性 InstanceKey“Nancy.Testing.Fakes.FakeNancyModule”的“modulePath”

这是我的测试:

public class when_a_user_logs_in_successfully
{
    static Browser _browser;
    static BrowserResponse _response;

     Establish context = () =>
         {
            var bootstrapper = new BlurtsBootsrapper();
            _browser = new Browser(bootstrapper); //throws exception here
        };

     Because of = () => _response = _browser.Get("/Login", with => with.HttpRequest());

     It should_return_a_successful_response = () => _response.Body.ShouldNotBeNull();
}

这是我的 BlurtsBootstrapper:

public class BlurtsBootsrapper : StructureMapNancyBootstrapper
{
    protected override void ApplicationStartup(StructureMap.IContainer container, Nancy.Bootstrapper.IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);

        container.Configure(x => x.AddRegistry<BlurtsRegistry>());
    }        
}

【问题讨论】:

  • 您能否提供更多有关堆栈跟踪的信息?另外,您是否在 BlurtsRegistry 中进行了更多注册?谢谢
  • 我最终能够重现它。似乎它正在尝试使用它最贪婪的构造函数创建 FakeNancyModule 的实例,并且由于它的类型是 string ,因此它在容器中没有解析的价值。将研究我们的解决方案
  • 我现在有什么办法可以解决吗?
  • 我尝试在基础 ctor 中为该模块提供一个 modulePath,但这没有用。
  • 您现在可以使用可配置的引导程序作为解决方法。它基于 TinyIoC,但无论如何它都可以让您测试您的路线。

标签: c# testing nancy


【解决方案1】:

我遇到了同样的问题并找到了this post,这让我找到了对我有用的答案

在您的引导程序中,添加以下内容:

container.Configure(x => {
    x.SelectConstructor(()=>new FakeNancyModule());
    x.AddRegistry<BlurtsRegistry>();
})

至少在 StructureMapBootstrapper 本身更新之前,这应该可以工作。

【讨论】:

  • 这对我有用。在项目中创建了一个继承自引导程序的新 NancyBootsrapper 并添加了 x.SelectConstructor。
【解决方案2】:

在 0.10 中,您必须通过覆盖 ConfigureRequestContainer(IContainer container, NancyContext context) 来做到这一点

看起来像这样:

protected override void ConfigureRequestContainer(IContainer container, NancyContext context)
{
    container.Configure(x =>
    {
        x.SelectConstructor(() => new FakeNancyModule());
    });
    base.ConfigureRequestContainer(container, context);
}

他们说他们会尝试在 0.11 版本中修复它

https://github.com/NancyFx/Nancy.Bootstrappers.StructureMap/issues/8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-04
    • 2020-10-03
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多