ASP.Net核心集成测试 [英] ASP.Net Core Integration Testing

查看:68
本文介绍了ASP.Net核心集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力进行与ASP.Net Core RC2一起使用的各种集成测试. 我已经创建了一个基本的Web项目,该项目在浏览器中可以正常运行,并按预期显示默认页面.然后,我用以下测试代码添加了一个新类(在同一项目中):

I'm struggling to get any sort of integration tests working with ASP.Net Core RC2. I have created a basic web project which runs fine in the browser showing the default page as expected. 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:

未找到视图索引".搜索了以下位置:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml

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

在此阶段,我正在使用MSTest而不是xUnit.不幸的是,将ContentRoot更改为重复"问题中建议的内容不能解决该问题.

I'm using MSTest as opposed to xUnit at this stage. Changing the ContentRoot to that suggested in the "duplicate" question unfortunately does not resolve the issue.

有人进行过集成测试吗?我尝试调整WebHostBuilder设置,但是没有运气.

Has anyone got integration tests working? I've tried tweaking the WebHostBuilder settings but with no luck.

推荐答案

我写了一篇有关集成测试的博客文章(不涉及MVC),"Snow Crash"评论了类似的问题.他们使用以下代码解决了未找到"错误:

I wrote a blog post on integration testing (not covering MVC), and 'Snow Crash' commented with a similar problem. 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>();

此博客文章位于 查看全文

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