我跟着this guide在dotnet core 2.1
中设置了集成测试项目我的项目配置如下:
$ dotnet sln list
Project reference(s)
--------------------
mtss-ws/mtss-ws.csproj
mtss-ws.integrationtests/mtss-ws.integrationtests.csproj
$ cd mtss-ws.integrationtests/
$ dotnet list mtss-ws.integrationtests.csproj reference
Project reference(s)
--------------------
..\mtss-ws\mtss-ws.csproj
在创建我的TestServer时,我必须加载配置表单,项目开始测试。我试过这个:
public PingController_RunShould() {
_server = new TestServer(new WebHostBuilder()
.ConfigureAppConfiguration((builderContext, config) => {
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.UseStartup<Startup>());
_client = _server.CreateClient();
}
但系统会尝试在appsettings.json
mtss-ws.integrationtests/bin/Debug/netcoreapp2.1/appsettings.json
文件
我尝试指定../mtss-ws/appsettings.json
但它没有用。
到目前为止,我可以解决它只是创建一个符号链接到所需的配置文件,但我想知道是否有一些更聪明的方法来解决它
答案 0 :(得分:0)
我一直在研究更多,并提出了这个解决方案:
private void SetUpClient()
{
_server = new TestServer(new WebHostBuilder()
.UseContentRoot(GetContentRootPath())
.ConfigureAppConfiguration((builderContext, config) => {
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.UseStartup<Startup>());
Client = _server.CreateClient();
}
private string GetContentRootPath() {
var testProjectPath = PlatformServices.Default.Application.ApplicationBasePath;
var relativePathToHostProject = @"../../../../mtss-ws";
return Path.Combine(testProjectPath, relativePathToHostProject);
}