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
{ }
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With