24

I've a basic .net core api web app and a unit test project that uses a TestServer to make http requests.

I've a TestStartup class that subclassed the Startup class in the api project.

If the Startup class is in the unit test project i get a 404 response. If the TestStartup class is moved to the api project i get a 200 reponse.

Api Project

Api.csproj

<PackageReference Include="Microsoft.AspNetCore.App" />

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

TestController.cs

public class TestController : ControllerBase
{
    [HttpGet("test")]
    public ObjectResult Get()
    {
        return Ok("data");
    }
}

Unit Test Project

Tests.csproj

<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />

Tests.cs

[TestFixture]
public class Tests
{
    [Test]
    public async Task Test()
    {
        var server = new TestServer(WebHost.CreateDefaultBuilder()
            .UseStartup<TestStartup>()
            .UseEnvironment("Development"));

        var response = await server.CreateClient().GetAsync("test");
    }
}

Startup.cs

public class TestStartup : Startup
{ }
3
  • I have just asked this question: stackoverflow.com/questions/53681935/… (before seeing yours). Did you find an answer? Could you answer my question? Thanks Commented Dec 8, 2018 at 15:00
  • I didn't find an answer. I rebuilt it from scratch and it worked. As far as I was concerned the implementations were exactly the same - one worked - the other didn't. There must have been something different - couldn't see what though. Commented Feb 19, 2019 at 12:17
  • not sure if this is the fix github.com/aspnet/AspNetCore/issues/4332 Commented May 30, 2019 at 13:10

4 Answers 4

59

In case someone experiences this targeting ASP.NET Core 3.0 or 3.1, don't forget to change the test project SDK to Microsoft.NET.Sdk.Web, i.e.

<Project Sdk="Microsoft.NET.Sdk.Web">

as per Test app prerequisites.

Sign up to request clarification or add additional context in comments.

4 Comments

Im having this exakt problem with net core 3.1 and I only have 'Microsoft.NET.Test.Sdk' in my test project. I searched in Nuget for 'Microsoft.NET.Sdk.Web' but couldnt find it? How am I supposed to change the sdk?
No, you just need to edit the *.csproj file and change the first line there: from: <Project Sdk="Microsoft.NET.Sdk"> to: <Project Sdk="Microsoft.NET.Sdk.Web"> And then magic will happen ;)
This is like gold, no more like hens teeth. Many thanks
Nice answer, it saved my day
13

You could try two options below:

  • Add AddApplicationPart to Startup.cs

    namespace IntegrationTestMVC
    {
    public class Startup
    {         
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {   
           services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("IntegrationTestMVC"))); //"IntegrationTestMVC" is your original project name
        }
    
  • Try to convert TestFixture to IClassFixture

        public class IntegrationTestMVCUnitTest : IClassFixture<WebApplicationFactory<TestStartup>>
    {
        private readonly HttpClient _client;
        private readonly WebApplicationFactory<TestStartup> _factory;
    
        public IntegrationTestMVCUnitTest(WebApplicationFactory<TestStartup> factory)
        {
    
            _factory = factory;
            _client = factory.CreateClient();
        }
    
        [Fact]
        public async Task IndexRendersCorrectTitle()
        {
            var response = await _client.GetAsync(@"/test");
        }
    
    }
    

For the second option, you could refer Integration tests in ASP.NET Core

3 Comments

Was this the solution to the original question?
services.AddMvc().AddApplicationPart(typeof(Startup).Assembly)
What if dont use AddMvc?
6

Adding a reference to Microsoft.AspNetCore.Mvc.Testing to the project file containing the test, as per the instructions in aspnetcore-2.2#test-app-prerequisities fixed the issue for me.

Comments

0

I had to update my nuget packages in test project to work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.