我使用Microsoft.AspNet.TestHost托管xunit集成测试。只要测试与ASP.NET-5解决方案处于相同的项目中,一切都按其应有的方式工作。
但是我想把测试放在一个单独的程序集中,将它们与解决方案分开。但是当我试图在单独的解决方案中运行测试时,我会发现一个错误,TestServer找不到视图。
Bsoft.Buchhaltung.Tests.LoginTests.SomeTest [FAIL]
System.InvalidOperationException : The view 'About' was not found. The following locations were searched:
/Views/Home/About.cshtml
/Views/Shared/About.cshtml.我猜TestServer正在相对于本地目录寻找视图。我怎样才能让它找到正确的项目路径呢?
发布于 2016-09-23 09:11:36
的答案是在RC1是当前版本的时候写的。现在(RTM 1.0.0 / 1.0.1),这变得更简单了:
public class TenantTests
{
private readonly TestServer _server;
private readonly HttpClient _client;
public TenantTests()
{
_server = new TestServer(new WebHostBuilder()
.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "..", "..", "..", "SaaSDemo.Web")))
.UseEnvironment("Development")
.UseStartup<Startup>());
_client = _server.CreateClient();
}
[Fact]
public async Task DefaultPageExists()
{
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.True(!string.IsNullOrEmpty(responseString));
}
}这里的关键是.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "..", "..", "..", "SaaSDemo.Web")))
该bin/debug/{platform-version}/{os-buildarchitecture}/位于测试程序集ApplicationBasePath文件夹中。您需要向上遍历树,直到您到达包含您的视图的项目为止。在我的案子里。SaasDemo.Tests与SaasDemo.Web位于同一个文件夹中,因此遍历5个文件夹是正确的。
发布于 2016-01-24 23:45:50
我这里有一个示例回购- https://github.com/mattridgway/ASPNET5-MVC6-Integration-Tests,它显示了修复(感谢大卫福勒)。
TL;DR -当设置TestServer时,您需要设置应用程序基本路径来查看其他项目,以便它能够找到视图。
发布于 2016-08-23 21:17:19
对于以后的参考,请注意,您现在可以像下面这样设置内容根目录:
string contentRoot = "path/to/your/web/project";
IWebHostBuilder hostBuilder = new WebHostBuilder()
.UseContentRoot(contentRoot)
.UseStartup<Startup>();
_server = new TestServer(hostBuilder);
_client = _server.CreateClient();https://stackoverflow.com/questions/34590142
复制相似问题