After upgrading from .NET Core 2.1 to 3.1 Views aren't being found when my app starts in E2E tests

Viewed 191

In Home controller I'm returning return View("Login", ReturnUrl);

In Views/Home/ there's Login.cshtml

Everything works fine when I manually start the app, but when I'm starting application in Selenium tests like that:

[Parallelizable]
[Test]
public async Task TEST()
{
    var info = this.ConnectionInfo[Configure()];
    foreach (var driver in GetDrivers())
    {
        var _context = GetDb(info.ConnectionString);
        driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);

        _context.Database.EnsureDeleted();
        _context.Database.EnsureCreated();

        driver.Navigate().GoToUrl(info.Address);
    }
}

public Guid Configure()
{
    var addressId = Guid.NewGuid();
    
    var port1 = FreeTcpPort();
    var port2 = FreeTcpPort();

    var dbFileGuid = Guid.NewGuid().ToString("N") + ".db";

    var config = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json")
                    .Build();

    config["Tests:ConnectionString"] = $"Filename={dbFileGuid}";

    _host = new WebHostBuilder()
                .UseKestrel()
                .UseConfiguration
                (
                    config
                )
                .UseUrls($"http://localhost:{port1}/", $"https://localhost:{port2}/")
                .UseStartup<TestsStartup>()
                .UseSerilog()
                .Build();

    _host.StartAsync();

    ConnectionInfo.TryAdd(addressId, new ConnectionInfo($"http://127.0.0.1:{port1}", dbFileGuid, _host));

    return addressId;
}

TestStartup is using the same methods to configure ConfigureServices and Configure as my real config, with the difference in DatabaseContext provider.

Here's what appears during tests

enter image description here

Here are my parts of Startup

services
    .AddControllersWithViews(x =>
    {
        x.Filters.Add(typeof(CustomExceptionFilter));
    })
    .AddNewtonsoftJson()
    .AddApplicationPart(typeof(HomeController).Assembly)
    .AddControllersAsServices()
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

services
    .AddRazorPages()
    .AddRazorRuntimeCompilation();



app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute
    (
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}"
    );
    endpoints.MapRazorPages();
});

What may cause that App is unable to find those Views when started from Tests meanwhile it managed to do it on older .NET Core / older ASP libs?

2 Answers

Well,

I managed to fix it in kinda hackly by telling Test project to copy Views folder from the app folder to Test's bin

That's what I added to the .csproj

<ItemGroup>
    <Content Include="..\MyApp\Views\**" Link="Views\%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="Always" />
</ItemGroup>

Change two things in the startup file: (Add MVC to your project pipeline)

services
    .AddControllersWithViews(x =>
    {
        x.Filters.Add(typeof(CustomExceptionFilter));
    })
    .AddNewtonsoftJson()
    .AddApplicationPart(typeof(HomeController).Assembly)
    .AddMvc().AddControllersAsServices();
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

Then change your endpoint like this: (it's default routing for MVC project)

app.UseEndpoints(endpoints =>
{
  endpoints.MapDefaultControllerRoute();
});
Related