C
C#7mo ago
fasadin

MassTransit unit test does not hit breakpoint in consumer

I have problem with writing an unit test that will check my consumer. It's not hitting breakpoint in my Consumer when I am debugging test code for test
[Theory]
[AutoData]
public async Task GivenUserCreatedEventIsSend_WhenProperlyEventIsHandled_ThenUserIsCreated(Guid userId)
{
await using var provider = new ServiceCollection()
.AddDbContext<ChatContext>(options => options.UseInMemoryDatabase("TestChatDatabase"))
.AddMassTransitTestHarness(cfg =>
{
cfg.AddConsumer<UserCreatedMessageHandler>();
})
.BuildServiceProvider(true);

using (var scope = provider.CreateScope())
{
var chatContext = scope.ServiceProvider.GetRequiredService<ChatContext>();

var harness = scope.ServiceProvider.GetRequiredService<ITestHarness>();
await harness.Start();

try
{
await harness.Bus.Publish<UserCreated>(new
{
Id = userId
});

Assert.True(await harness.Consumed.Any<UserCreated>());


await using (chatContext)
{
// Verify changes in the database
var entity = await chatContext.Users.FirstOrDefaultAsync( /* Add conditions to find the entity */);
entity.Should().NotBeNull();
// Add additional assertions to verify the state of the entity in the database
}
}
finally
{
await harness.Stop();
}
}
}
[Theory]
[AutoData]
public async Task GivenUserCreatedEventIsSend_WhenProperlyEventIsHandled_ThenUserIsCreated(Guid userId)
{
await using var provider = new ServiceCollection()
.AddDbContext<ChatContext>(options => options.UseInMemoryDatabase("TestChatDatabase"))
.AddMassTransitTestHarness(cfg =>
{
cfg.AddConsumer<UserCreatedMessageHandler>();
})
.BuildServiceProvider(true);

using (var scope = provider.CreateScope())
{
var chatContext = scope.ServiceProvider.GetRequiredService<ChatContext>();

var harness = scope.ServiceProvider.GetRequiredService<ITestHarness>();
await harness.Start();

try
{
await harness.Bus.Publish<UserCreated>(new
{
Id = userId
});

Assert.True(await harness.Consumed.Any<UserCreated>());


await using (chatContext)
{
// Verify changes in the database
var entity = await chatContext.Users.FirstOrDefaultAsync( /* Add conditions to find the entity */);
entity.Should().NotBeNull();
// Add additional assertions to verify the state of the entity in the database
}
}
finally
{
await harness.Stop();
}
}
}
what's confusing it's that assertions passes fine, but my class UserCreatedMessageHandler it's never called / hit by breakpoint (ctor nor Consume method)
1 Reply
fasadin
fasadinOP7mo ago
it looks like some mocked Consumer it's reading message and cosuming it. But how?
Want results from more Discord servers?
Add your server