【发布时间】:2019-09-09 08:53:01
【问题描述】:
我正在使用NUnit 编写单元测试。这是我的被测类的样子
public class CustomerService
{
private readonly IConfiguration _configuration;
// ctor
CustomerService(IConfiguration configuration)
{
}
}
上述类的单元测试。
public class CustomerServiceTests
{
private readonly IConfiguration _configuration;
CustomerServiceTests()
{
_configuration = GetConfiguration();
}
public static IConfiguration GetConfiguration(string outputPath="")
{
return new ConfigurationBuilder()
.SetBasePath(outputPath)
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.Build();
}
}
现在我需要获取 StartUp (API) 项目中存在的 appsettings.json 目录的路径
这是我的项目目录的样子
-
客户项目
-customer.api
-customer.services
-customer.services.test
如何获取包含appsettings.json 的customer.api 项目的路径?
【问题讨论】:
-
您的代码刚刚使用
SetBasePath指定了该路径。CustomerServiceTests甚至不会编译,因为GetConfiguration期望outputPath丢失 -
@PanagiotisKanavos 对不起!我没找到你
-
您要查找的基本路径是
outputPath,您刚刚在SetBasePath(outputPath)调用中指定了它。
标签: c# asp.net-core dependency-injection nunit filepath