公共交通如何测试/调试没有响应的消费者?



在测试项目中,我想执行CallbackReceivedEventHandler中的代码所以我试着";发布"给这个消费者一些东西,但最终消费者没有被调用。我做错了什么?

我必须使消费者响应成为某种东西,然后在测试项目中使用RequestClient。。。这不是一个好的解决方法。。。消费者不应响应任何

请帮助

应用程序项目

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
}

测试项目

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)

很可能在将消息发送给使用者之前,您的测试已经完成。消息是异步传递的。

在测试中,发布之后,您可以等待测试线束上的InactivityTask,以确保消息已发送。或者您可以等待消息被消费。两种方法都有效,但其中一种方法更具体,确保消息发布。

await bus.Publish(input);
await harness.Consumed.Any<CallbackReceivedEvent>();

最新更新