更新:我用
解决了我的问题var directoryName = Path.GetDirectoryName(typeof (Bootstrapper).Assembly.CodeBase);
与
var directoryName = AppDomain.CurrentDomain.BaseDirectory;
所以现在测试工作正常。无论如何,如果有人可以使原始代码正常工作(可能是在解释为什么 Bootstraper 比我的解决方案更好 :),我会留下问题悬而未决,这样我就可以理解为什么在这里使用
原始问题
我刚刚在 C# 中使用 Nancyfx,但上周我才重新开始使用 C# 工作,可能我遗漏了很多与 dot.net 和 C# 相关的概念,将研究这些内容周末
尝试为 nancy 模块创建一些测试(我使用 NancyFx 的 Nunit 和辅助测试类)我遇到的问题是这些测试必须使用我为其编写测试的项目的路径(例如我在 TestProject 中编写了这些测试,并且正在测试 SelfHostNancyProject)。 所以我需要明确地解决这个问题(实际上我手动复制了 TestProject 的 Views 目录中的项目 SelfHostNancyProject 的 Views 目录)。 但是,即使使用自动生成后命令复制这些目录/文件也不是最好的方法。
我发现了这个解决方案 https://github.com/NancyFx/Nancy/wiki/Nancy-Testing-View-Location 但是在我的代码中复制类 TestingRootPathProvider 的代码我有错误
The type or namespace Bootstrapper cannot be found
public class TestingRootPathProvider : IRootPathProvider
{
private static readonly string RootPath;
static TestingRootPathProvider()
{
var directoryName = Path.GetDirectoryName(typeof Bootstrapper).Assembly.CodeBase);
if (directoryName != null)
{
var assemblyPath = directoryName.Replace(@"file:\", string.Empty);
RootPath = Path.Combine(assemblyPath, "..", "..", "..", "Escape.Web");
}
}
public string GetRootPath()
{
return RootPath;
}
}
但是我用谷歌闲逛我找不到解决方案。我看到这似乎与应用程序初始化有关 What are Bootstrappers/Bootstrapping in C#
但无论如何我不明白我必须执行哪个导入(如果有的话)来解决问题或者可能是我遗漏了什么。
最佳答案
我用替换解决了我的问题
var directoryName = Path.GetDirectoryName(typeof (Bootstrapper).Assembly.CodeBase);
与
var directoryName = AppDomain.CurrentDomain.BaseDirectory;
关于c# - NancyFx 测试 : The type or namespace Bootstrapper cannot be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46487335/