Skip to content

[Feature]: Testing Helpers Package #226

@eneshoxha

Description

@eneshoxha

Title: Add Cortex.Mediator.Testing package with test doubles and assertions

Labels: enhancement, mediator, new-package

Body:

Problem

Testing code that depends on IMediator requires consumers to set up mocks (Moq, NSubstitute) for every test. Testing individual handlers requires manually constructing pipeline chains. There are no first-party test utilities.

Proposed Solution

Create a Cortex.Mediator.Testing package with:

FakeMediator -- Records all dispatched messages for assertions:

var mediator = new FakeMediator();
mediator.SetupCommandResult<CreateUserCommand, Guid>(cmd => Guid.NewGuid());

await sut.DoWork(mediator);

mediator.ShouldHaveSent<CreateUserCommand>(cmd => cmd.Name == "Alice");
mediator.ShouldHavePublished<UserCreatedNotification>();
mediator.ShouldNotHaveSent<DeleteUserCommand>();

HandlerTestFixture<THandler> -- Isolated handler testing with automatic DI:

var fixture = new HandlerTestFixture<CreateUserCommandHandler>();
fixture.Register<IUserRepository>(mockRepo.Object);

var result = await fixture.HandleAsync(new CreateUserCommand { Name = "Alice" });

Pipeline testing -- Assert behavior execution order:

var pipeline = PipelineAssert.ForCommand<CreateUserCommand, Guid>(services);
pipeline.ShouldExecuteInOrder(
    typeof(LoggingBehavior<,>),
    typeof(ValidationBehavior<,>),
    typeof(CreateUserCommandHandler));

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestfeatureThis label is in use for minor version increments

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions