question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asp .Net Core 3.0 adding a custom Startup class in tests causes that the Views are not served anymore

See original GitHub issue

I test an Asp .Net Core 3.0 MVC application. This a basic sample app create with the command dotnet new mvc. In the xunit test project I test returning of the view Index from the HomeController. The test looks like this:

public class HomeControllerTests
{
    [Fact]
    public async Task ReturnsView()
    {
        const string testProjectDir = "ViewsTestingTests";
        var factory = new TestServerFactory();
        var client = factory.WithWebHostBuilder(builder =>
        {
            builder.UseSolutionRelativeContentRoot(testProjectDir);
            builder.ConfigureTestServices(services =>
            {
                services.AddControllersWithViews()
                    .AddApplicationPart(typeof(Startup).Assembly);
            });
        }).CreateClient();

        var resultIndex = await client.GetAsync("/");
        resultIndex.StatusCode.Should().Be(200);
        var content = await resultIndex.Content.ReadAsStringAsync();
        content.Should().Contain("Welcome");
    }
}

public class TestServerFactory : WebApplicationFactory<TestStartup>
{
    protected override IWebHostBuilder CreateWebHostBuilder()
    {
        return WebHost.CreateDefaultBuilder(null)
            .UseStartup<TestStartup>();
    }
}

public class TestStartup : Startup
{
    public TestStartup(IConfiguration configuration) : base(configuration)
    {
    }
}

I use a test startup class because I’ll be overriding some methods. When I use this TestStartup class the test doesn’t pass with the following reason:

The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml

But if I use the Startup class from the main project like this:

public class TestServerFactory : WebApplicationFactory<Startup>
{
    protected override IWebHostBuilder CreateWebHostBuilder()
    {
        return WebHost.CreateDefaultBuilder(null)
            .UseStartup<Startup>();
    }
}

And create Client like this:

var client = factory.WithWebHostBuilder(builder =>
            {
                builder.UseSolutionRelativeContentRoot(testProjectDir);
            }).CreateClient();

Then the test pass. Of course I copied the whole Views folder to the test project. What is interesting the same test used to work in Asp. Net Core 2.2.

Using builder.UseContentRoot inside factory.WithWebHostBuilder and setting a path to the test project folder doesn’t help. Please help me how to approach this problem.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pikoscielniakcommented, Feb 10, 2020

@mykola384 for me even better solution. I must only add the package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation. Thank you.

1reaction
mykola384commented, Feb 10, 2020

I fixed it by adding

services .AddMvc() .AddRazorRuntimeCompilation() .AddApplicationPart(typeof(TTestStartup).Assembly);

But thanks for the suggestion!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asp .Net Core 3.0 adding a custom Startup class in tests ...
Asp .Net Core 3.0 adding a custom Startup class in tests causes that the Views are not served anymore · Ask Question.
Read more >
Integration tests in ASP.NET Core
ASP.NET Core supports integration tests using a unit test framework with a ... Use the custom CustomWebApplicationFactory in test classes.
Read more >
Using custom startup class with ASP.NET Core integration ...
My previous post demonstrated how to use custom appsettings.js file with integration tests in ASP.NET Core. But in practice it's not enough ...
Read more >
Avoiding Startup service injection in ASP.NET Core 3
In this post I describe the changes to dependency injection in your Startup class when upgrading an ASP.NET Core 2.x app to .NET...
Read more >
ASP.NET Core — Autofac 7.0.0 documentation
In the ConfigureContainer method of your Startup class register things directly into an Autofac ContainerBuilder . The IServiceProvider will automatically be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found