简体   繁体   English

ASP.Net核心集成测试

[英]ASP.Net Core Integration Testing

I'm struggling to get any sort of integration tests working with ASP.Net Core RC2. 我正在努力使用ASP.Net Core RC2进行任何类型的集成测试。 I have created a basic web project which runs fine in the browser showing the default page as expected. 我创建了一个基本的Web项目,它在浏览器中运行正常,显示了预期的默认页面。 I then added a new class (in the same project) with the following test code: 然后,我使用以下测试代码添加了一个新类(在同一个项目中):

[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 test does not pass, with the following exception: 测试没有通过,但有以下例外:

The view 'Index' was not found. 未找到“索引”视图。 The following locations were searched: 搜索了以下位置:
/Views/Home/Index.cshtml /Views/Home/Index.cshtml
/Views/Shared/Index.cshtml /Views/Shared/Index.cshtml

I'm using MSTest as opposed to xUnit at this stage. 我在这个阶段使用MSTest而不是xUnit。 Changing the ContentRoot to that suggested in the "duplicate" question unfortunately does not resolve the issue. 不幸的是,将ContentRoot更改为“重复”问题中建议的内容并不能解决问题。

Has anyone got integration tests working? 有没有人进行集成测试? I've tried tweaking the WebHostBuilder settings but with no luck. 我试过调整WebHostBuilder设置,但没有运气。

I wrote a blog post on integration testing (not covering MVC), and 'Snow Crash' commented with a similar problem. 我写了一篇关于集成测试的博客文章(不包括MVC),而“Snow Crash”则发表了类似问题的评论。 They solved the 'not found' error with the following code: 他们使用以下代码解决了“未找到”错误:

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

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

The blog post is here Introduction to integration testing with xUnit and TestServer in ASP.NET Core . 博客文章介绍了使用ASP.NET Core中的xUnit和TestServer进行集成测试的简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM