title: Integration test for ASP.NET Core 6 web API throws System.InvalidOperationException
body: <p>I'm trying to understand how to do integration tests on ASP.NET Core 6 web API controllers. I've tried following all the <a href="https://gist.github.com/davidfowl/0e0372c3c1d895c3ce195ba983b1e03d#testing-with-webapplicationfactorytestserver" rel="nofollow noreferrer">guides</a>, SO posts and recommendations I could find but for some reason I keep hitting errors that aren't mentioned in the guides.</p>
  <p>EventControllerTests.cs</p>
  <pre><code>namespace UnitTests.ProjectToBeTested.Controllers
  {
      public class EventControllerTests
      {
          [Fact]
          public async Task EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync()
          {
  
              // Arrange
              var application = new WebApplicationFactory&lt;Program&gt;();
  
              var client = application.CreateClient();
              ...
  </code></pre>
  <p>ProjectToBeTested.csproj</p>
  <pre><code>...
      &lt;ItemGroup&gt;
          &lt;InternalsVisibleTo Include=&quot;IntegrationTests&quot; /&gt;
      &lt;/ItemGroup&gt;
  ...
  </code></pre>
  <p>This throws the following when running the test:</p>
  <blockquote>
  <p>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.</p>
  <p>Stack Trace:  WebApplicationFactory<code>1.CreateWebHostBuilder() WebApplicationFactory</code>1.EnsureServer()
  WebApplicationFactory<code>1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory</code>1.CreateDefaultClient(Uri baseAddress,
  DelegatingHandler[] handlers)
  WebApplicationFactory<code>1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory</code>1.CreateClient()
  EventControllerTests.EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync() line 17
  --- End of stack trace from previous location ---</p>
  </blockquote>
  <p><a href="https://stackoverflow.com/questions/61159115/asp-net-core-testing-no-method-public-static-ihostbuilder-createhostbuilders">This SO post</a> shows up as a possible solution but is using pre-6 using a Startup class. What would the .NET 6 solution be?</p>
  <p>If I instead follow the <a href="https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory" rel="nofollow noreferrer">&quot;Basic tests with the default WebApplicationFactory&quot;-guide</a> I can't even build the solution because of the test class constructor throwing</p>
  <blockquote>
  <p>Error CS0051  Inconsistent accessibility: parameter type
  'WebApplicationFactory' is less accessible than method
  'EventControllerTests.EventControllerTests(WebApplicationFactory)'   IntegrationTests
  C:\...\EventControllerTests.cs</p>
  </blockquote>