Table of Contents

Class TestActorProvider

Namespace
Trellis.Testing.Fakes
Assembly
Trellis.Testing.dll

Mutable IActorProvider for integration and authorization testing. Stores the current actor in an AsyncLocal<T> so parallel tests using overlapping WithActor(Actor) scopes never interfere with each other.

public sealed class TestActorProvider : IActorProvider
Inheritance
TestActorProvider
Implements
Inherited Members
Extension Methods

Examples

var actorProvider = new TestActorProvider("admin", "Orders.Read", "Orders.Write");

// Temporarily switch to a restricted user
await using var scope = actorProvider.WithActor("user-1", "Orders.Read");
var result = await mediator.Send(new CreateOrderCommand());
result.Should().BeFailure(); // missing Orders.Write
// scope disposes → actor reverts to admin

Constructors

TestActorProvider(string, params string[])

Initializes a new TestActorProvider with an actor created from the specified user ID and permissions.

public TestActorProvider(string userId, params string[] permissions)

Parameters

userId string

The unique identifier of the actor.

permissions string[]

The permissions granted to the actor.

TestActorProvider(Actor)

Initializes a new TestActorProvider with the specified Actor.

public TestActorProvider(Actor actor)

Parameters

actor Actor

The initial (default) actor returned when no scope is active.

Methods

GetCurrentActorAsync(CancellationToken)

Returns the current actor. Throws if no authenticated user exists (authentication should be handled before the request reaches the mediator pipeline).

public Task<Actor> GetCurrentActorAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Token to cancel the asynchronous operation.

Returns

Task<Actor>

The current authenticated Actor.

WithActor(string, params string[])

Temporarily replaces the current actor with one created from the specified user ID and permissions. Returns a TestActorScope that restores the previous actor on dispose.

public TestActorScope WithActor(string userId, params string[] permissions)

Parameters

userId string

The unique identifier of the actor.

permissions string[]

The permissions granted to the actor.

Returns

TestActorScope

A disposable scope that restores the previous actor.

WithActor(Actor)

Temporarily replaces the current actor for the current async flow. Returns a TestActorScope that restores the previous actor on dispose.

public TestActorScope WithActor(Actor actor)

Parameters

actor Actor

The actor to use for the duration of the scope.

Returns

TestActorScope

A disposable scope that restores the previous actor.