Created
December 16, 2025 13:57
-
-
Save dj-nitehawk/abf3fd08bae544ee3bcafb5c5f487c4a to your computer and use it in GitHub Desktop.
Integration testing an endpoint that executes a command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed record MyCommand(string Message) : ICommand; | |
| sealed class MyCommandHandler : ICommandHandler<MyCommand> | |
| { | |
| public Task ExecuteAsync(MyCommand command, CancellationToken ct) | |
| => Task.CompletedTask; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Sut : AppFixture<Program> | |
| { | |
| protected override void ConfigureServices(IServiceCollection s) | |
| { | |
| s.RegisterTestCommandReceivers(); // register the test command receiver service | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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