In the test project I wanted to execute the codes in the CallbackReceivedEventHandler
So I tried to "Publish" something to this consumer but in the end the consumer is not invoked. What did I do wrong?
I had to make the consumer response something then use the RequestClient in the test project... it is not a good workaround... the consumer should not response anything
please help
Application Project
public class CallbackReceivedEventHandler : IConsumer<CallbackReceivedEvent>
{
public async Task Consume(ConsumeContext<CallbackReceivedEvent> context)
{
//Breakpoint is here
...
await context.RespondAsync(new CallbackReceivedEventResponse()); // I want to remove this thing
}
Test Project
provider = new ServiceCollection()
.AddMassTransitInMemoryTestHarness(cfg =>
{
configurator.AddConsumers(typeof(CallbackReceivedEventHandler));
})
.AddGenericRequestClient()
.BuildServiceProvider(true);
var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.Start();
var bus = provider.GetRequiredService<IBus>();
CallbackReceivedEvent input = new()
// NOT WORK
await bus.Publish(input); //KO! the consumer is not called (breakpoint in Consume is not hit)
// WORK!
var requester = bus.CreateRequestClient<CallbackReceivedEvent>();
await requester.GetResponse<CallbackReceivedEventResponse>(input); //OK! the consumer is called (breakpoint in Consume is hit)
CodePudding user response:
It's likely that your test is completing before the message has been dispatched to the consumer. Messages are delivered asynchronously.
In your test, after the publish, you can await the InactivityTask on the test harness to ensure the message was dispatched. Or you could wait for the message to be consumed. Either approach works, but one is more specific ensuring that the message was published.
await bus.Publish(input);
await harness.Consumed.Any<CallbackReceivedEvent>();