WebApplicationFactory引发错误,表明ASP.NET Core集成测试中不存在contentRootPath [英] WebApplicationFactory throws error that contentRootPath does not exist in ASP.NET Core integration test

查看:131
本文介绍了WebApplicationFactory引发错误,表明ASP.NET Core集成测试中不存在contentRootPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core项目,其中包含一些简单的Razor页面和一个Web API控制器.

I have a ASP.NET Core project with some simple Razor pages and a Web API controller.

我使用清洁架构作为起点.我已经重命名了项目名称,删除了MVC内容,并添加了一些自己的代码.一切运行正常.

I'm using Clean Architecture as a starting point. I've renamed the project names, removed the MVC stuff and added some of my own code. Everything runs and works.

但是,集成测试在调用factory.CreateClient()时会引发以下错误:

However, the integration tests throw the following error when calling factory.CreateClient():

Test Name:  ToDo.Tests.Integration.Web.HomeControllerIndexShould.ReturnViewWithCorrectMessage
Test FullName:  ToDo.Tests.Integration.Web.HomeControllerIndexShould.ReturnViewWithCorrectMessage
Test Source:    C:\Source\Utopia\tests\ToDo.Tests\Integration\Web\HomeControllerIndexShould.cs : line 18
Test Outcome:   Failed
Test Duration:  0:00:00,001

Result StackTrace:  
at Microsoft.AspNetCore.Hosting.Internal.HostingEnvironmentExtensions.Initialize(IHostingEnvironment hostingEnvironment, String contentRootPath, WebHostOptions options)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateServer(IWebHostBuilder builder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
   at ToDo.Tests.Integration.Web.HomeControllerIndexShould..ctor(CustomWebApplicationFactory`1 factory) in C:\Source\Utopia\tests\ToDo.Tests\Integration\Web\HomeControllerIndexShould.cs:line 14
Result Message: 
System.ArgumentException : The content root 'C:\Source\Utopia\ToDo.Web' does not exist.
Parameter name: contentRootPath

我尝试配置自定义builder.UseContentRoot和builder.UseSolutionRelativeContentRoot> WebApplicationFactory ,但是无论我为ContentRoot方法使用什么值,它都会抛出相同的错误.

I've tried configuring the custom WebApplicationFactory by using builder.UseContentRoot and builder.UseSolutionRelativeContentRoot but it keeps throwing the same error no matter what values I use for the ContentRoot methods.

我不知道为什么当Clean Architecture示例中的测试正常进行时,我的测试为什么失败.我也不知道该如何解决.

I don't know why my tests are failing while the one in the Clean Architecture sample are working. I also don't know how to fix it.

任何指针都将受到高度赞赏!

Any pointers are highly appreciated!

推荐答案

此方法对我有用

        var client = _factory
            .WithWebHostBuilder(builder => builder.UseSolutionRelativeContentRoot("relative/path/of/project/under/test"))
            .CreateClient();

请参见

See How the test infrastructure infers the app content root path for more information.

这篇关于WebApplicationFactory引发错误,表明ASP.NET Core集成测试中不存在contentRootPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆