Class TestActorProvider
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
userIdstringThe unique identifier of the actor.
permissionsstring[]The permissions granted to the actor.
TestActorProvider(Actor)
Initializes a new TestActorProvider with the specified Actor.
public TestActorProvider(Actor actor)
Parameters
actorActorThe 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
cancellationTokenCancellationTokenToken to cancel the asynchronous operation.
Returns
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
userIdstringThe unique identifier of the actor.
permissionsstring[]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
actorActorThe actor to use for the duration of the scope.
Returns
- TestActorScope
A disposable scope that restores the previous actor.