I have a fairly straightforward example and want to unit test an endpoint that has not response. I have seen examples of using this and it works as expected.
var harness = new InMemoryTestHarness();
harness.Consumer<FooBarConsumer>();
await harness.Start();
await harness.InputQueueSendEndpoint.Send<FooCommand>(new FooCommand());
Assert.IsTrue(await harness.Consumed.Any<FooCommand>());
However, I would like to do this with ServiceProvider so that I can setup and mock dependencies. This is what I have so far
await using var provider = new ServiceCollection()
.AddMassTransitTestHarness(cfg =>
{
cfg.UsingInMemory((ctx, mem) =>
{
mem.ConfigureEndpoints(ctx);
});
cfg.AddConsumer<FooBarConsumer>();
})
.BuildServiceProvider(true);
If I try doing the following I get an error stating this hasn’t been registered.
var harness = provider.GetRequiredService<InMemoryTestHarness>();
The example states to do this instead
var harness = provider.GetRequiredService<ITestHarness>();
However by doing this I don’t get access to InputQueueSendEndpoint
. It forces me down the route of request and response but there is no output for this endpoint.
var client = harness.GetRequestClient<FooCommand>();
await client.GetResponse<FooBar>(new FooCommand());
I guess question is, is it possible to test a consumer who receives a command only with no output using InMemoryTestHarness
but also setup its dependencies?