I am trying to make clean integration tests for ASP .NET WebApi.
This is the webapplication factory class.
The program class its using in the extend is an internal from the API project. (Done with InternalsVisibleTo
)
internal class DozerWebApplicationFactory : WebApplicationFactory<Program>
{
//Omitted not useful
}
In tests I can do this:
public class QuotesControllerTests
{
private readonly DozerWebApplicationFactory _application;
private readonly HttpClient _httpClient;
public QuotesControllerTests()
{
// Arrange/Setup
_application = new DozerWebApplicationFactory();
_httpClient = _application.CreateClient();
}
}
But I would like to do this using the IClassFixture class:
public class QuotesControllerTests : IClassFixture<DozerWebApplicationFactory>
{
private readonly DozerWebApplicationFactory _application;
private readonly HttpClient _httpClient;
public QuotesControllerTests(DozerWebApplicationFactory application)
{
// It doesn't work because this constructor is public (tests need to be public) but the Factory is internal due to program being internal as stated above.
_application = application;
_httpClient = _application.CreateClient();
}
}
However this last approach does not because this constructor is public (tests need to be public) but the Factory is internal due to program being internal as stated above.
CS0051 Inconsistent accessibility: parameter type 'DozerWebApplicationFactory' is less accessible than method 'QuotesControllerTests.QuotesControllerTests(DozerWebApplicationFactory)'
What can I do to make this work with IClassFixture
?
One option is to create a sort of "holder" in the test project - that can be a public class, but contain a reference to the internal class:
public class DozerFactoryHolder
{
internal DozerWebApplicationFactory Factory { get; set; }
}
That can then be used as the type argument and constructor parameter:
public class QuotesControllerTests : IClassFixture<DozerFactoryHolder>
{
private readonly DozerWebApplicationFactory _application;
public QuotesControllerTests(DozerFactoryHolder holder)
{
_application = holder.Factory;
...
}
}