XUnit Net Core Web API 集成测试:"The ConnectionString property has not been initialized."
XUnit Net Core Web API Integration Test: "The ConnectionString property has not been initialized."
只是想为 NET Core Web 构建一个集成测试项目 API。
因此,我遵循了几个示例,包括这个 (https://dotnetcorecentral.com/blog/asp-net-core-web-api-integration-testing-with-xunit/),自然而然地,我 运行 进入了问题。当我 运行 简单的 GET 测试时,我得到一个异常:
"System.InvalidOperationException : The ConnectionString property has not been initialized."
如有任何帮助,我们将不胜感激。
对于server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
,您需要像
一样手动配置appsettings.json
路径
var server = new TestServer(WebHost.CreateDefaultBuilder()
.UseContentRoot(@"D:\Edward\SourceCode\AspNetCore\Tests\IntegrationTestMVC")
// This is the path for project which needs to be test
.UseStartup<Startup>()
);
为方便起见,我建议您尝试 Basic tests with the default WebApplicationFactory。
The WebApplicationFactory constructor infers the app content root path by searching for a WebApplicationFactoryContentRootAttribute on the assembly containing the integration tests with a key equal to the TEntryPoint assembly System.Reflection.Assembly.FullName. In case an attribute with the correct key isn't found, WebApplicationFactory falls back to searching for a solution file (*.sln) and appends the TEntryPoint assembly name to the solution directory. The app root directory (the content root path) is used to discover views and content files.
参考:How the test infrastructure infers the app content root path
我必须在派生的 WebApplicationFactory
中覆盖 CreateHostBuilder
以添加连接字符串的配置(因为它是从用户机密中读取的)。
public class CustomApplicationFactory : WebApplicationFactory<Sedab.MemberAuth.Startup>
{
protected override IHostBuilder CreateHostBuilder()
{
var initialData = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("ConnectionStrings:DefaultConnection", "test")
};
return base.CreateHostBuilder().ConfigureHostConfiguration(config => config.AddInMemoryCollection(initialData));
}
}
只是想为 NET Core Web 构建一个集成测试项目 API。 因此,我遵循了几个示例,包括这个 (https://dotnetcorecentral.com/blog/asp-net-core-web-api-integration-testing-with-xunit/),自然而然地,我 运行 进入了问题。当我 运行 简单的 GET 测试时,我得到一个异常: "System.InvalidOperationException : The ConnectionString property has not been initialized."
如有任何帮助,我们将不胜感激。
对于server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
,您需要像
appsettings.json
路径
var server = new TestServer(WebHost.CreateDefaultBuilder()
.UseContentRoot(@"D:\Edward\SourceCode\AspNetCore\Tests\IntegrationTestMVC")
// This is the path for project which needs to be test
.UseStartup<Startup>()
);
为方便起见,我建议您尝试 Basic tests with the default WebApplicationFactory。
The WebApplicationFactory constructor infers the app content root path by searching for a WebApplicationFactoryContentRootAttribute on the assembly containing the integration tests with a key equal to the TEntryPoint assembly System.Reflection.Assembly.FullName. In case an attribute with the correct key isn't found, WebApplicationFactory falls back to searching for a solution file (*.sln) and appends the TEntryPoint assembly name to the solution directory. The app root directory (the content root path) is used to discover views and content files.
参考:How the test infrastructure infers the app content root path
我必须在派生的 WebApplicationFactory
中覆盖 CreateHostBuilder
以添加连接字符串的配置(因为它是从用户机密中读取的)。
public class CustomApplicationFactory : WebApplicationFactory<Sedab.MemberAuth.Startup>
{
protected override IHostBuilder CreateHostBuilder()
{
var initialData = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("ConnectionStrings:DefaultConnection", "test")
};
return base.CreateHostBuilder().ConfigureHostConfiguration(config => config.AddInMemoryCollection(initialData));
}
}