In .net core 6, startup.cs has been removed, the integration testing I used before doesn't work anymore as is. I need to do an update.
In my API (program.cs), I added:
[assembly: InternalsVisibleTo("constructionCompanyAPI.IntegrationTests")]
and here is the Integration.Tests csproj:
I created a ConstructionCompanyControllersTests.cs like:
public class ConstructionCompanyControllerTests
{
private HttpClient _client;
public ConstructionCompanyControllerTests()
{
_client = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
var dbContextOptions = services
.SingleOrDefault(service => service.ServiceType == typeof(DbContextOptions<ConstructionCompanyDbContext>));
services.Remove(dbContextOptions);
services.AddDbContext<ConstructionCompanyDbContext>(options => options.UseInMemoryDatabase("ConstructionCompanyDb"));
});
})
.CreateClient();
}
[Theory]
[InlineData("?pageNumber=1&pageSize=10")]
[InlineData("?pageNumber=1&pageSize=5")]
public async Task GetAll_WithQueryParameters_ReturnsOkResult(string queryParams)
{
// act
var response = await _client.GetAsync("/api/constructionCompany" + queryParams);
// assert
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
}
When I launch the API, every thing works correctly, I'm to see my controller in Swagger and call the get with a code 200 returns.
So each time the test fails here:
var response = await _client.GetAsync("/api/constructionCompany" + queryParams);
The response always get a 404 error (not found).
Expected response.StatusCode to be HttpStatusCode.OK {value: 200}, but found HttpStatusCode.NotFound {value: 404}.
What should I do to make it works correctly?
Thanks
Edit: The Entire program.cs file:
Program
? I would have expected that to beStartup
– Disconsolate