XUnit Net Core Web API Integration Test: “The ConnectionString property has not been initialized.”

不羁的心 提交于 2020-01-06 08:46:15

问题


Just trying to build an Integration Test project for a NET Core Web API. So I've followed a few examples, including this one (https://dotnetcorecentral.com/blog/asp-net-core-web-api-integration-testing-with-xunit/) and naturally, I run into issues. When I run the simple GET test I get an exception: "System.InvalidOperationException : The ConnectionString property has not been initialized."

Any help would be appreciated.


回答1:


For server = new TestServer(new WebHostBuilder().UseStartup<Startup>());, you need to manually configure the appsettings.json path like

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>()
    );

For a convenience way, I would suggest you try 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.

Reference:How the test infrastructure infers the app content root path



来源:https://stackoverflow.com/questions/53826747/xunit-net-core-web-api-integration-test-the-connectionstring-property-has-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!