Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 将asp.net 5测试放置在单独的程序集中_Unit Testing_Asp.net Core_Xunit - Fatal编程技术网

Unit testing 将asp.net 5测试放置在单独的程序集中

Unit testing 将asp.net 5测试放置在单独的程序集中,unit-testing,asp.net-core,xunit,Unit Testing,Asp.net Core,Xunit,我使用Microsoft.AspNet.TestHost来主持xunit集成测试。只要测试与asp.net-5解决方案在同一个项目中,一切都会正常工作。 但是我想把测试放在一个单独的组件中,把它们从解决方案中分离出来。但是当我尝试在单独的解决方案中运行测试时,我得到一个错误,TestServer找不到视图 Bsoft.Buchhaltung.Tests.LoginTests.SomeTest [FAIL] System.InvalidOperationException : The view

我使用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正在相对于本地目录查找视图。我怎样才能让它找到正确的项目路径呢?

我这里有一个回购示例-它显示了修复方法(感谢David Fowler)


TL;DR-设置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.
string contentRoot = "path/to/your/web/project";
IWebHostBuilder hostBuilder = new WebHostBuilder()
    .UseContentRoot(contentRoot)
    .UseStartup<Startup>();
_server = new TestServer(hostBuilder);
_client = _server.CreateClient();
string contentRoot=“path/to/your/web/project”;
IWebHostBuilder主机生成器=新的WebHostBuilder()
.UseContentRoot(contentRoot)
.UseStartup();
_服务器=新的测试服务器(hostBuilder);
_client=_server.CreateClient();

马特·里奇韦的答案是在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));

    }
}
公共类租户
{
专用只读测试服务器(TestServer);;
私有只读HttpClient\u客户端;
公屋住户()
{
_服务器=新的TestServer(新的WebHostBuilder()
.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.Application.ApplicationBasePath,“…”、“.”、“.”、“、”、“.”、“、SaaSDemo.Web”))
.环境(“发展”)
.UseStartup());
_client=_server.CreateClient();
}
[事实]
公共异步任务DefaultPageExists()
{
var response=wait_client.GetAsync(“/”);
response.EnsureSuccessStatusCode();
var responseString=await response.Content.ReadAsStringAsync();
Assert.True(!string.IsNullOrEmpty(responseString));
}
}
这里的关键点是
.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.Application.ApplicationBasePath,“…”,“,”,“,”,“SaaSDemo.Web”))


ApplicationBasePath位于测试程序集bin/debug/{platform version}/{os buildarchitecture}/文件夹中。您需要向上遍历该树,直到到达包含视图的项目。就我而言
SaasDemo.Tests
SaasDemo.Web
位于同一文件夹中,因此遍历5个文件夹是正确的。

为了使重写的启动类正常工作,我必须做的另一个更改是将IHostingEnvironment对象中的ApplicationName设置为Web项目的实际名称(Web程序集的名称)

当TestStartup位于不同的程序集中并重写原始启动类时,这是必需的。在我的案例中仍然需要UseContentRoot


如果名称未设置,则我总是得到一个404 not found。

您是否找到了此问题的解决方案Nope,我仍然需要解决方案:(它对我不起作用…我使用1.1版。它表示“缺少一个或多个编译引用”。建议可能的原因包括“buildOptions”下缺少“PreserveComilationContext”属性;在应用程序的project.json中。但它是在my.csproj文件中设置的。在单独的程序集中使用test进行测试,并且我的路径是相对的,也可以工作。谢谢:_server=new TestServer(new WebHostBuilder().UseContentRoot(@.\..\..\..\my.Real.Api”).UseStartup());