Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aspnet core integration test returning 404

I'm trying to implement the integration test for my app following the tutorial from: 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);
    }
}

However, when I'm trying to run the integration test, it gives me the following error:

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

It seems to be a common problem when separating the integration test from the MVC application.

Here's the startup.cs too

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've found some work arounds, that says to include the AddApplicationPart in the Startup.cs, but it's still not working.

I'm not sure if it's not working because I'm using .NET Core 2.0. I appreciate any tips.

like image 677
Thiago Custodio Avatar asked Sep 13 '17 16:09

Thiago Custodio


1 Answers

I had similar problem like you have and I solved it by adding appsettings.json file path to WebHostBuilder(). I implemented like following.

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()
            );
like image 195
hasan Avatar answered Oct 18 '22 04:10

hasan