profile
viewpoint

Ask questionsMigrate Asp.Net Core 2.2 Integration Tests to 3.0 - The TestServer constructor was not called with a IWebHostBuilder so IWebHost is not available.

We had basic Api integration tests setup as following. When trying to update to 3.0 I get the following error.

System.InvalidOperationException : The TestServer constructor was not called with a IWebHostBuilder so IWebHost is not available.

How to convert to support 3.0?

public class MainAPITests : BaseAPITests
{
	public MainAPITests(WebApplicationFactory<Startup> factory) : base(factory)
	{
	}

	// tests here
}

public class BaseAPITests : IClassFixture<WebApplicationFactory<Startup>>
{
	public HttpClient Client { get; }
	public TestServer Server { get; }

	public BaseAPITests(WebApplicationFactory<Startup> factory)
	{
		// ensure we use "Test" environment
		factory = factory.WithWebHostBuilder(builder => builder.UseStartup<Startup>().UseEnvironment("Test"));

		Client = factory.CreateDefaultClient();
		Server = factory.Server;

		// get context from startup where we check if test or not and use in memory
		var catalogDB = Server.Host.Services.GetService<CatalogDbContext>();

		// handle additional details
	}
}
aspnet/AspNetCore

Answer questions javiercn

@aherrick Thanks for contacting us.

Instead of doing factory.Server.Services you can simply do factory.Services. The error you are seeing is likely due to the move to generic host. The Services property will do the right thing.

If that doesn't work, let us know.

useful!

Related questions

HTTP Error 500.31 - ANCM Failed to Find Native Dependencies hot 8
ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY when clean webapi template
MissingMethodException: Method not found Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder
UseStaticFiles with StaticFileOptions/FileExtensionContentTypeProvider breaks server side Blazor hot 3
HTTP Error 500.30 - ANCM In-Process Start Failure hot 3
With Asp.net core 3.0 SignInManager.IsSignedIn(User) allways reuturn false?but in 2.2 it works fine.Is that a debug? hot 3
CSS Isolation in Blazor Components hot 3
How to retrieve the HttpContext from Blazor server-side ? hot 3
Oauth2 authentication erroring with "The oauth state was missing or invalid." hot 3
Failed to gracefully shutdown application (IIS Hang) hot 3
BadHttpRequestException: Reading the request body timed out due to data arriving too slowly hot 2
HTTP Error 500.0 - ANCM In-Process Handler Load Failure hot 2
Method not found: 'Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex(System.Linq.Expressions.Expression`1<System.Func`2<System.__Canon,System.Object>>)'. hot 2
MissingMethodException at SignInManager ctor after update hot 2
BadHttpRequestException: Reading the request body timed out due to data arriving too slowly hot 2
Github User Rank List