-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathIntegrationTestBase.cs
More file actions
42 lines (36 loc) · 1.81 KB
/
IntegrationTestBase.cs
File metadata and controls
42 lines (36 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using TUnit.AspNetCore;
namespace EssentialCSharp.Web.Tests;
public abstract class IntegrationTestBase : WebApplicationTest<WebApplicationFactory, Program>
{
/// <summary>
/// Creates an HTTP client with redirect following enabled.
/// NOTE: This bypasses TUnit trace correlation because <see cref="TracedWebApplicationFactory{T}"/>
/// does not expose a <c>CreateClient(WebApplicationFactoryClientOptions)</c> overload.
/// Use <see cref="TUnit.AspNetCore.TracedWebApplicationFactory{T}.CreateClient()"/> for all
/// other tests where AllowAutoRedirect=false is acceptable.
/// </summary>
protected HttpClient CreateRedirectFollowingClient() =>
Factory.Inner.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = true });
public T InServiceScope<T>(Func<IServiceProvider, T> action)
{
using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
return action(scope.ServiceProvider);
}
public void InServiceScope(Action<IServiceProvider> action)
{
using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
action(scope.ServiceProvider);
}
public async Task<T> InServiceScopeAsync<T>(Func<IServiceProvider, Task<T>> action)
{
using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
return await action(scope.ServiceProvider);
}
public async Task InServiceScopeAsync(Func<IServiceProvider, Task> action)
{
using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
await action(scope.ServiceProvider);
}
}