【问题标题】:aspnet core integration test returning 404aspnet 核心集成测试返回 404
【发布时间】:2017-09-13 16:50:55
【问题描述】:

我正在尝试按照以下教程为我的应用实施集成测试:https://docs.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);
    }
}

但是,当我尝试运行集成测试时,它给了我以下错误:

“消息:System.InvalidOperationException:视图'索引'不是 成立。搜索了以下位置: /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。我很感激任何提示。

【问题讨论】:

  • 我觉得你需要验证内容根路径
  • @Nkosi 它也不起作用。如您所见,我正在硬编码 ContentRoot 的路径,即使这样,也没有成功...
  • 看看这里是怎么设置路径的stackoverflow.com/a/37844252/5233410
  • 对我来说绝对是 ContentRootPath 问题。
  • @ThiagoCustodio 你解决问题了吗?

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


【解决方案1】:

我遇到了与您类似的问题,我通过将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()
            );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2018-03-28
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多