How to setup Asp.net Core TestServer with Azure Function v4 for Integration Tests

Viewed 53

Trying to setup TestServer form Asp.Net Core manually because WebApplicationFactory is not available for Azure functions. This is for Integration tests in memory.

I have created my own Startup for Integration testing where I use Sqllite in memory to test services and repositories.

Which works fine, but I want to be able to use TestServer from Asp.Net Core into Azure Functions, tried to Setup using, but throws an exception if I use ConfigureWebHost and ConfigureServices:

System.ObjectDisposedException : Cannot access a disposed object. Object name: 'IServiceProvider'.

ConfigureWebHost((webBuilder) =>
{
    webBuilder.UseTestServer();
})
    public BaseIntegrationTest()
    {
        TestHost = CreateHostBuilder().Build();
        Task.Run(() => TestHost.RunAsync());
    }

    public IHost TestHost { get; }

    public static IHostBuilder CreateHostBuilder(string[] args = null) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((services) =>
            {
                services.AddHttpContextAccessor();

                services.AddDomain();

                services.AddDbContext<HubDbContext>(options
                    => options.UseSqlite("Filename=:memory:",
                        (options) =>
                        {
                        }));

                services.AddRepositories();
            });
0 Answers
Related