【问题标题】:How do I explicitly specify where a Nancy view is located?如何明确指定 Nancy 视图的位置?
【发布时间】:2014-01-16 01:00:34
【问题描述】:

首先我知道问题出在哪里,我只是不太了解南希,不知道如何解决它。

作为 appharbor 构建过程的一部分,我的单元测试失败了。当 NCrunch 执行相同的测试时,它也会失败。但是,当由 VS2012 执行时,它工作正常。

测试看起来像这样:

[Test]
public void Get_Root_Should_Return_Status_OK()
{
    // Given
    var browser = new Browser(new Bootstrapper());

    // When
    var result = browser.Get("/");

    // Then
    Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}

处理"/" 路由的HomeModule 部分如下所示:

Get["/"] = _ => View["home.sshtml"];

home.sshtml 位于 Views 文件夹中。

如果我将上面的内容替换为:

Get["/"] = _ => "Hello World!;

然后测试变为绿色。

很明显,问题是在 NCrunch 和 appharbor 中运行测试时,找不到 home.sshtml 文件。

我如何明确告诉南希文件在哪里?

PS 正在将视图文件复制到输出目录。

PPS 我也尝试过明确告诉 Nancy 视图的位置,但这也不起作用。

protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
    var directoryInfo = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
    if (directoryInfo != null)
        Environment.CurrentDirectory = directoryInfo.FullName;
    Conventions.ViewLocationConventions.Add((viewName, model, viewLocationContext) => String.Concat("Views/", viewName));
}

【问题讨论】:

  • 我假设 Bootstrapper 是您主项目中的引导程序?里面有什么东西可以破坏它吗?您是使用 sshtml 文件作为嵌入式资源还是正常使用?您不应该需要 ApplicationStartup 中的 mods,所以我确信发生了一些简单的事情。流行于jabbr.net/#/rooms/nancyfx
  • 我已将视图嵌入到程序集中作为解决方法。通过现在到处测试。

标签: unit-testing nunit appharbor nancy


【解决方案1】:

问题是由于 NCrunch 在编译并复制 bin 目录以运行测试时没有将视图复制到输出目录。

您需要做的是将视图设置为始终复制,然后在您的单元测试项目中添加一个 IRootPathProvider 实现:

public class StaticPathProvider : IRootPathProvider
{
    public static string Path { get; set; }

    public string GetRootPath()
    {
        return Path;
    }
}

(不完全确定路径,我不记得了,认为它只是执行程序集的位置)

并在您的引导程序中注册以进行单元测试。

var browserParser = new Browser(with =>
{
  ...
  with.RootPathProvider<StaticPathProvider>();
  ...
});

缺点是部署时需要从/bin 目录中删除视图目录。


另一种方法是做你已经做过的事情,嵌入你的观点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2015-03-02
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多