0

SO when I did integration tests for api's, I used tio have a xunit project and used Microsoft.AspNetcore.Mvc.Testing.

There I used a WebApplicationFactory<namespace.Startup>().

According to microsoft docs they provide roughly the same:

 // Arrange
    _server = new TestServer(new WebHostBuilder()
       .UseStartup<Startup>());
    _client = _server.CreateClient();

found here: api integration tests

However since .net6.0 came out when creating an api project and other projects aswell, they don't seem to have a startup class anymore, all is embedded in the program.cs file, But program.cs file doesn't contain a class either, so i'm a little bit stuck on what to use in my webapplicationfactory<namesapce.startup> -> since there is no startup anymore

Any idea what to do here?

Edit: program.cs of my api (with controllers)

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();



public partial class Program { }

my tests:

public class ApiTests
    {
        
        private readonly HttpClient client;
        private readonly TestServer _server;

        private const string url = "https://localhost/api/network";

        public ApiTests()
        {
            _server = new TestServer(WebApplicationFactory<Network_Monitor_Agent.Program>);   
        }

        [Fact]
        public async Task GetRequest_Should_Return_Success_Information()
        {
            var response = await client.GetAsync(url);

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

        }
    }
7
  • 1
    Does this answer your question? How to do integration testing for .NET 6 Web Api projects using WebApplicationFactory? Commented Mar 17, 2022 at 6:55
  • 1
    ah yes that does answer my question thanks, I'll test later Commented Mar 17, 2022 at 17:25
  • this probably answers my question , but i'm stuck in where to place the include. Currently I have placed the itemgroup within the api project and inserted my tests project as name, however I still don't have program in my intellisense Commented Mar 19, 2022 at 10:40
  • Would you be able to add more of your code to the question please? Commented Mar 20, 2022 at 1:35
  • I have cloned the test project in the other issue, but explained there instead of adding an include reference, He made a public partial class program Commented Mar 20, 2022 at 9:42

0

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.