I'm trying to understand how to do integration tests on ASP.NET Core 6 web API controllers. I've tried following all the guides, SO posts and recommendations I could find but for some reason I keep hitting errors that aren't mentioned in the guides.
EventControllerTests.cs
namespace UnitTests.ProjectToBeTested.Controllers
{
public class EventControllerTests
{
[Fact]
public async Task EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync()
{
// Arrange
var application = new WebApplicationFactory<Program>();
var client = application.CreateClient();
...
ProjectToBeTested.csproj
...
<ItemGroup>
<InternalsVisibleTo Include="IntegrationTests" />
</ItemGroup>
...
This throws the following when running the test:
Message: System.InvalidOperationException : No method 'public static IHostBuilder CreateHostBuilder(string[] args)' or 'public static IWebHostBuilder CreateWebHostBuilder(string[] args)' found on 'Program'. Alternatively, WebApplicationFactory`1 can be extended and 'CreateHostBuilder' or 'CreateWebHostBuilder' can be overridden to provide your own instance.
Stack Trace: WebApplicationFactory
1.CreateWebHostBuilder() WebApplicationFactory
1.EnsureServer() WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory
1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory
1.CreateClient() EventControllerTests.EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync() line 17 --- End of stack trace from previous location ---
This SO post shows up as a possible solution but is using pre-6 using a Startup class. What would the .NET 6 solution be?
If I instead follow the "Basic tests with the default WebApplicationFactory"-guide I can't even build the solution because of the test class constructor throwing
Error CS0051 Inconsistent accessibility: parameter type 'WebApplicationFactory' is less accessible than method 'EventControllerTests.EventControllerTests(WebApplicationFactory)' IntegrationTests C:\...\EventControllerTests.cs
CodePudding user response:
I can't reproduce this. I created two new projects from the command line on .NET 6 RC1 with
dotnet new webapi -o webapi1
dotnet new xunit -o test1
dotnet new sln
Web API project
The only change I made to the Web API project was to add this to the project file:
<ItemGroup>
<InternalsVisibleTo Include="test1" />
</ItemGroup>
Program.cs
remained unchanged:
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "webapi2", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "webapi2 v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Test project
In the test project I added a reference to webapi1
and the Microsoft.AspNetCore.Mvc.Testing
package.
I changed Unit1.cs
to
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
using webapi2;
namespace test1;
public class UnitTest1
{
[Fact]
public void Test1()
{
using var app = new WebApplicationFactory<Program>();
using var client=app.CreateClient();
}
}
The projects compiled and the test run succesfully.
CodePudding user response:
Solution:
This was due to the Microsoft.AspNetCore.Mvc.Testing package for the test project using the wrong version (it was using version 5.*). Make sure that you use a version suitable for .NET 6. As of now there is a 6.0.0-rc.2.21480.10 version that works for me.