5

Tôi đang cố gắng để thực hiện các thử nghiệm tích hợp cho ứng dụng của tôi sau khi hướng dẫn từ: https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testingaspnet thử nghiệm tích hợp lõi trở về 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); 
    } 
} 

Tuy nhiên, khi tôi đang cố gắng để chạy thử nghiệm hội nhập, nó mang lại cho tôi lỗi sau:

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

Có vẻ như đây là vấn đề thường gặp khi tách kiểm tra tích hợp khỏi ứng dụng MVC.

Đây là startup.cs quá

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

Tôi đã tìm thấy một số quanh công việc, nói rằng để bao gồm các AddApplicationPart trong Startup.cs, nhưng nó vẫn không làm việc.

Tôi không chắc chắn nếu nó không hoạt động vì tôi đang sử dụng .NET Core 2.0. Tôi đánh giá cao bất kỳ lời khuyên nào.

+0

Tôi nghĩ bạn cần phải xác minh đường dẫn gốc của nội dung – Nkosi

+0

@Nkosi Nó cũng không hoạt động. Như bạn có thể thấy, tôi đang khó mã hóa đường dẫn cho ContentRoot và thậm chí với điều đó, không thành công ... –

+1

Hãy xem cách đường dẫn được đặt ở đây https://stackoverflow.com/a/37844252/5233410 – Nkosi

Trả lời

0

Tôi gặp sự cố tương tự như bạn gặp phải và tôi đã giải quyết vấn đề bằng cách thêm appsettings.json đường dẫn tệp tới WebHostBuilder(). Tôi thực hiện như sau.

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ác vấn đề liên quan