Using SendLocal in integration tests

I would like to create tests for our NServiceBus configuration by creating an endpoint and send messages to it (to be able to test different scenarios).

To do that I’ve created a handler and then use ep.SendLocal to send a message to it.

[Fact]
public async Task SomeTest()
{
            // Our own builder that wraps the NServiceBus configuration
            var sut = new EndpointBuilder("Myname");
            sut.RegisterServices(x =>
            {
                x.RegisterType<MyService>().AsSelf().SingleInstance();
                x.RegisterType<MyMessageHandler>().AsImplementedInterfaces();
            });

            // Will call Endpoint.Start(_endpointConfiguration) and return the ep
            var ep = await sut.BuildEndpoint(ConnectionString);

            // Creates a new static TaskCompletionSource
            MyMessageHandler.Reset();

            await ep.SendLocal(new MyMessage());

            // waits on the tcs which will be completed when the handler is invoked
            await MyMessageHandler.Wait(TimeSpan.FromSeconds(5));

            await ep.Stop();
}

The handler is never invoked, do NSB wait for something before it actually sends and processes the message?

Hi

What do ep and endPoint variables represent? An endpoint needs to be started before it processes the messages. Does the BuildEndpoint method call Endpoint.Start or just Endpoint.Create?

Szymon

Sorry, typo in the code. It should just be ep. It represents a IEndpointInstance (from Endpoint.Start(endpointConfiguration);).

In that case it should work correctly. Can you also share the code in the BuildEndpoint method?