skip to Main Content

I saw IClassFixture<WebApplicationFactory> in .net core 3 for IntegrationTests, now I want to write an integration test for a project which is .et6, what should I use instead of IClassFixture<WebApplicationFactory> in .net 6 for IntegrationTests ?

Here is what I tried:

public class TicketControllerTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly HttpClient _client;
    public TicketControllerTests(WebApplicationFactory<Program> factory)
    {
        _client = factory.CreateDefaultClient();
    }

    [Fact]
    public async Task Get()
    {
        var response = await _client.GetAsync("/Tickets/Test");

        //Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}

Here I get this error:

enter image description here

2

Answers


  1. You can use IClassFixture in all versions of .Net.

    I think your error comes from Program.cs, put your codes here to help you.

    Login or Signup to reply.
  2. I got this error when I was trying to use unconfigured startup (default) in IClassFixture. I think my solution will solve your question indirectly.

    I solved this problem by change my program.cs. first, I add this code as ProgramBuilder in my api:

    public class ProgramBuilder
    {
        public static IHostBuilder CreateHostBuilder<T>(params string[] args)
        where T : Startup
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(defaults => defaults.UseStartup<T>());
        }
    }
    

    then in my program.cs I add this code:

    var myHost = ProgramBuilder.CreateHostBuilder<Startup>(args).Build();
        myHost.Run();
    

    I used this link it helped me alot:
    https://github.com/dotnet/aspnetcore/issues/40361

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search