ASP.Net 核心集成测试

ASP.Net Core Integration Testing

我正在努力获得与 ASP.Net Core RC2 一起工作的任何类型的集成测试。 我创建了一个基本的 Web 项目,它在浏览器中运行良好,显示了预期的默认页面。然后我添加了一个新的 class (在同一个项目中),测试代码如下:

[TestClass]
public class HomeControllerTests
{
    private HttpClient client;

    [TestInitialize]
    public void Initialize()
    {
        // Arrange
        var host = new WebHostBuilder()
           .UseEnvironment("Development")
           .UseKestrel()
           .UseContentRoot(Directory.GetCurrentDirectory())
           .UseIISIntegration()
           .UseStartup<Startup>();

        TestServer server = new TestServer(host);

        client = server.CreateClient();
    }


    [TestMethod]
    public async Task CheckHomeIndex()
    {
        string request = "/";

        var response = await client.GetAsync(request);

        response.EnsureSuccessStatusCode();

        Assert.IsTrue(true);
    }
}

测试未通过,但有以下异常:

The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml

现阶段我使用的是 MSTest 而不是 xUnit。不幸的是,将 ContentRoot 更改为 "duplicate" 问题中建议的内容并不能解决问题。

有人进行集成测试吗?我试过调整 WebHostBuilder 设置但没有成功。

我写了一篇关于集成测试的博客 post(不涉及 MVC),'Snow Crash' 评论了类似的问题。他们使用以下代码解决了 'not found' 错误:

var path = PlatformServices.Default.Application.ApplicationBasePath;
var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory> ));

var builder = new WebHostBuilder()
   .UseContentRoot(setDir)
   .UseStartup<TStartup>();

博客 post 在这里 Introduction to integration testing with xUnit and TestServer in ASP.NET Core