Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created December 16, 2025 13:57
Show Gist options
  • Select an option

  • Save dj-nitehawk/abf3fd08bae544ee3bcafb5c5f487c4a to your computer and use it in GitHub Desktop.

Select an option

Save dj-nitehawk/abf3fd08bae544ee3bcafb5c5f487c4a to your computer and use it in GitHub Desktop.
Integration testing an endpoint that executes a command
sealed record MyCommand(string Message) : ICommand;
sealed class MyCommandHandler : ICommandHandler<MyCommand>
{
public Task ExecuteAsync(MyCommand command, CancellationToken ct)
=> Task.CompletedTask;
}
sealed record MyRequest(string Msg);
[HttpGet("/endpoint"), AllowAnonymous]
sealed class MyEndpoint : Endpoint<MyRequest>
{
public override async Task HandleAsync(MyRequest r, CancellationToken c)
{
var cmd = new MyCommand(r.Msg);
await cmd.ExecuteAsync(c); // "test command receiver" is used to determine if command was executed by the endpoint.
await Send.OkAsync("all good!");
}
}
public class Sut : AppFixture<Program>
{
protected override void ConfigureServices(IServiceCollection s)
{
s.RegisterTestCommandReceivers(); // register the test command receiver service
}
}
public class MyTests(Sut App) : TestBase<Sut>
{
[Fact]
public async Task Endpoint_Executes_Correct_Command()
{
//obtain a "test command receiver" for you command type
var receiver = App.Services.GetTestCommandReceiver<MyCommand>();
//call the endpoint
var (rsp, _) = await App.Client.GETAsync<MyEndpoint, MyRequest, EmptyResponse>(new("yello!"));
rsp.IsSuccessStatusCode.ShouldBeTrue();
//check if "test command receiver" received the command that's supposed to be executed by the endpoint.
var received = await receiver.WaitForMatchAsync(e => e.Message == "yello!", timeoutSeconds: 3, ct: Cancellation);
received.Any().ShouldBeTrue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment