Class ProtoExecutionContextExtensions

Namespace
ProtoTest.AspNetCore
Assembly
ProtoTest.AspNetCore.dll

Provides ASP.NET Core extension methods on ProtoExecutionContext.

public static class ProtoExecutionContextExtensions
Inheritance
ProtoExecutionContextExtensions
Inherited Members

Methods

ApplicationServices<TProgram>(ProtoExecutionContext, string?)

Gets the test's scope over the application under test, created on first use and disposed when the test ends. Scoped domain services - repositories, command handlers, a DbContext - resolve from it, so a provisioner can create data through the application's own logic.

public static IServiceProvider ApplicationServices<TProgram>(this ProtoExecutionContext context, string? name = null) where TProgram : class

Parameters

context ProtoExecutionContext

The active test execution context.

name string

The registered server name. Defaults to the application selected for the test, or "Default".

Returns

IServiceProvider

Type Parameters

TProgram

The entry point class of the ASP.NET Core application.

CreateServerScope<TProgram>(ProtoExecutionContext, string?)

Creates a dedicated IServiceScope from the specified ASP.NET Core server's DI container. The caller owns the returned scope and must dispose it.

public static IServiceScope CreateServerScope<TProgram>(this ProtoExecutionContext context, string? name = null) where TProgram : class

Parameters

context ProtoExecutionContext

The active test execution context.

name string

The registered server name. Defaults to the application selected for the test, or "Default".

Returns

IServiceScope

Type Parameters

TProgram

The entry point class of the ASP.NET Core application.

Override<TService>(ProtoExecutionContext, Func<TService>, string?)

Substitutes a service in the application under test for this test, building it with the given factory when the application resolves it. The test runs against a dedicated server built with the substitution, so the run's shared server never sees it and the next test starts clean.

public static void Override<TService>(this ProtoExecutionContext context, Func<TService> factory, string? name = null) where TService : class

Parameters

context ProtoExecutionContext

The active test execution context.

factory Func<TService>

The factory the application resolves for the test.

name string

The server to substitute on. When omitted, the test's selected application is used, falling back to Default - the same rule the server accessors resolve by.

Type Parameters

TService

The service contract the application resolves.

Override<TService>(ProtoExecutionContext, TService, string?)

Substitutes a service in the application under test for this test, serving the given instance to the application. The test runs against a dedicated server built with the substitution, so the run's shared server never sees it and the next test starts clean. The replacement is a singleton of the dedicated server: apply the override before the test first resolves application services.

public static void Override<TService>(this ProtoExecutionContext context, TService instance, string? name = null) where TService : class

Parameters

context ProtoExecutionContext

The active test execution context.

instance TService

The instance the application resolves for the test.

name string

The server to substitute on. When omitted, the test's selected application is used, falling back to Default - the same rule the server accessors resolve by.

Type Parameters

TService

The service contract the application resolves.

Override<TService, TImplementation>(ProtoExecutionContext, string?)

Substitutes a service in the application under test for this test, resolving an implementation type through the application's container. The test runs against a dedicated server built with the substitution, so the run's shared server never sees it and the next test starts clean.

public static void Override<TService, TImplementation>(this ProtoExecutionContext context, string? name = null) where TService : class where TImplementation : class, TService

Parameters

context ProtoExecutionContext

The active test execution context.

name string

The server to substitute on. When omitted, the test's selected application is used, falling back to Default - the same rule the server accessors resolve by.

Type Parameters

TService

The service contract the application resolves.

TImplementation

The concrete replacement the container constructs.

ServerFactory<TProgram>(ProtoExecutionContext, string?)

Retrieves the underlying WebApplicationFactory<TEntryPoint> registered under the specified name.

public static WebApplicationFactory<TProgram> ServerFactory<TProgram>(this ProtoExecutionContext context, string? name = null) where TProgram : class

Parameters

context ProtoExecutionContext

The active test execution context.

name string

The registered server name. Defaults to the application selected for the test, or "Default".

Returns

WebApplicationFactory<TProgram>

Type Parameters

TProgram

The entry point class of the ASP.NET Core application.

ServerService<TProgram, TService>(ProtoExecutionContext, string?)

Resolves a required service from the application under test through the test's own scope, so scoped services work and are disposed with the test.

public static TService ServerService<TProgram, TService>(this ProtoExecutionContext context, string? name = null) where TProgram : class where TService : notnull

Parameters

context ProtoExecutionContext

The active test execution context.

name string

The registered server name. Defaults to the application selected for the test, or "Default".

Returns

TService

Type Parameters

TProgram

The entry point class of the ASP.NET Core application.

TService

The service type to resolve.

TryServerFactory<TProgram>(ProtoExecutionContext, string?)

Gets the in-process server factory for an application, or null when the application is not hosted in-process (published or container-backed runs). This is the lookup an integration uses to prefer an in-process path only when one exists.

public static WebApplicationFactory<TProgram>? TryServerFactory<TProgram>(this ProtoExecutionContext context, string? name = null) where TProgram : class

Parameters

context ProtoExecutionContext
name string

Returns

WebApplicationFactory<TProgram>

Type Parameters

TProgram