将asp.net 5测试放在单独的程序集中 [英] placing asp.net 5 tests in separate assembly

查看:76
本文介绍了将asp.net 5测试放在单独的程序集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Microsoft.AspNet.TestHost托管xunit集成测试.只要测试与asp.net-5-solution处于同一项目中,一切就可以正常进行.
但是我想将测试放在单独的程序集中,以将它们与解决方案分开.但是,当我尝试在单独的解决方案中运行测试时,出现错误,TestServer无法找到视图.

I use Microsoft.AspNet.TestHost to host xunit integration tests. As Long as the tests are in the very same Project as the asp.net-5-solution everything works as it should.
But I'd like to place the tests into a separate assembly, to separate them from the solution. But when I try to run the tests in the separate solution I get an error, TestServer can't find the views.

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正在查看相对于本地目录的视图.如何获取正确的项目路径呢?

I guess the TestServer is looking relative to the local Directory for the views. How can I get it to look in the correct project path instead?

推荐答案

当RC1是当前版本时,Matt Ridgway的答案已被编写.现在(RTM 1.0.0/1.0.1)变得更简单了:

Matt Ridgway's answer was written, when RC1 was the current version. Now (RTM 1.0.0 / 1.0.1) this has gotten simpler:

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")))

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

The ApplicationBasePath is in your test assemblies bin/debug/{platform-version}/{os-buildarchitecture}/ folder. You need to traverse that tree upwards, until you reach your project containing your views. In my case. SaasDemo.Tests is in the same folder as SaasDemo.Web, so traversing up 5 folders was the right amount.

这篇关于将asp.net 5测试放在单独的程序集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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