c# - aspnet 核心集成测试返回 404

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

我正尝试按照以下教程为我的应用实现集成测试:https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing

public class CreditCardApplicationShould
{
    [Fact]
    public async Task RenderApplicationForm()
    {
        var builder = new WebHostBuilder()
            .UseContentRoot(@"C:\Users\usuario\source\repos\CreditCardApp\CreditCardApp")
            .UseEnvironment("Development")
            .UseStartup<CreditCardApp.Startup>()
            .UseApplicationInsights();

        var server = new TestServer(builder);

        var client = server.CreateClient();

        var response = await client.GetAsync("/Apply/Index");

        response.EnsureSuccessStatusCode();

        var responseString = await response.Content.ReadAsStringAsync();

        Assert.Contains("New Credit Card Application", responseString);
    }
}

但是,当我尝试运行集成测试时,出现以下错误:

"Message: System.InvalidOperationException : The view 'Index' was not found. The following locations were searched: /Views/Apply/Index.cshtml /Views/Shared/Index.cshtml"

这似乎是将集成测试与 MVC 应用程序分离时的常见问题。

这也是 startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
        CurrentEnvironment = env;
    }

    public IConfiguration Configuration { get; }

    private IHostingEnvironment CurrentEnvironment { get;  }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddApplicationPart(typeof(ApplyController).GetTypeInfo().Assembly);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

我找到了一些解决方法,即在 Startup.cs 中包含 AddApplicationPart,但它仍然不起作用。

我不确定它是否不起作用,因为我使用的是 .NET Core 2.0。我很感激任何提示。

最佳答案

我遇到了与您类似的问题,我通过将 appsettings.json 文件路径添加到 WebHostBuilder() 来解决它。我实现如下。

var builder = new WebHostBuilder()
            .UseContentRoot(@"C:\Users\usuario\source\repos\CreditCardApp\CreditCardApp")
            .UseEnvironment("Development")
            .UseStartup<CreditCardApp.Startup>()
            .UseApplicationInsights()
            .UseConfiguration(new ConfigurationBuilder()
                    .SetBasePath(YourProjectPath) // @"C:\Users\usuario\source\repos\CreditCardApp\CreditCardApp"
                    .AddJsonFile("appsettings.json")
                    .Build()
            );

关于c# - aspnet 核心集成测试返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203001/

相关文章:

javascript - 防止浏览器控制从特定域加载 javascript

c# - 在不同的工作簿上运行 Excel 宏

.net - 缺少 “ASP.NET Core Web Application (.NET Framework)” 模板

c# - DynamicRouteValueTransformer 不存在

windows - 处理 .net 核心中的窗口锁定/解锁事件

c# - 在 C# 中读取和写入 XML 文件的值

javascript - 如何在 Javascript 中检查 ASP.NET C# Request.IsAuthenticated?

c# - 在 ASP.NET 5 中为 Web API Controller 模拟 HttpContext

c# - 有什么方法可以将迭代的 id 传递给 jquery?

.net-core - .NET Core 依赖树