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
{ }