Aspnet core integration test returning 404

I am trying to perform an integration test for my application after the tutorial: 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); } } 

However, when I try to run the integration test, it causes the following error:

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

This seems to be a common problem when separating an integration test from an MVC application.

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

I found several jobs that talk about including AddApplicationPart in Startup.cs, but it still doesn't work.

I am not sure if it does not work because I am using .NET Core 2.0. I appreciate any advice.

+5
source share
1 answer

I had a similar problem like yours, and I solved it by adding the appsettings.json path to WebHostBuilder() . I implemented as shown below.

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

Source: https://habr.com/ru/post/1271767/


All Articles