【问题标题】:ASP.Net Core Integration TestingASP.Net 核心集成测试
【发布时间】:2016-06-15 09:12:07
【问题描述】:

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

[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);
    }
}

测试没有通过,有以下例外:

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

在这个阶段,我使用的是 MSTest,而不是 xUnit。不幸的是,将 ContentRoot 更改为“重复”问题中建议的内容并不能解决问题。

有人让集成测试工作吗?我试过调整WebHostBuilder 设置但没有运气。

【问题讨论】:

标签: c# asp.net-core integration-testing


【解决方案1】:

我写了一篇关于集成测试的博文(不包括 MVC),“Snow Crash”评论了一个类似的问题。他们使用以下代码解决了“未找到”错误:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2019-02-20
    相关资源
    最近更新 更多