5

próbuję wdrożyć test integracyjny dla mojej aplikacji po samouczku: https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testingASPNET testu integracji rdzeń powrocie 404

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

jednak, gdy próbuję uruchomić test integracyjny, to daje mi następujący błąd:

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

Wydaje się być częstym problemem podczas oddzielania testu integracji od aplikacji MVC.

Oto startup.cs zbyt

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?}"); 
     }); 
    } 
} 

znalazłem kilka arounds pracy, który mówi do włączenia AddApplicationPart w Startup.cs, ale nadal nie działa.

Nie jestem pewien, czy to nie działa, ponieważ używam .NET Core 2.0. Doceniam wszelkie wskazówki.

+0

Myślę, że musisz zweryfikować ścieżkę dostępu do treści – Nkosi

+0

@Nkosi To też nie działa. Jak widzisz, ciężko koduję ścieżkę do ContentRoot, a nawet z tym, bez powodzenia ... –

+1

Zobacz, jak ustawia się ścieżkę tutaj https://stackoverflow.com/a/37844252/5233410 – Nkosi

Odpowiedz

0

Miałem podobny problem jak ty i rozwiązałem go, dodając ścieżkę pliku appsettings.json do WebHostBuilder(). Zaimplementowałem, jak poniżej.

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() 
      );