Open Pastafarian opened 6 months ago
Quickly glancing at the code, it's possible that your background service has already been instantiated and is running in the web host by the time your tests run, so it's too late for any changes to service registrations to take effect.
Thanks for your suggestion @martincostello, even when I remove the service and add it again, I still get the same problem.
[Fact]
public async Task FirstTest1()
{
await SetupRabbitMq();
_fixture.BuildConsumerHttpClient((service) =>
{
var descriptor = service.Single(s => s.ImplementationType == typeof(WorkerService));
service.Remove(descriptor);
service.AddHostedService<WorkerService>();
service.Replace(ServiceDescriptor.Transient<IMessage, MessageTest1>());
return true;
});
BasicPublish(new PublicationAddress(_rabbitMqClientConsumerConfiguration.ExchangeType, _rabbitMqClientConsumerConfiguration.LoggingExchangeName, string.Empty), "MessageTest1"u8.ToArray());
}
Any suggestions on how I could fix this?
@davidfowl any chance you'd be able to take a quick look?
Thanks!
Is there an existing issue for this?
Describe the bug
I have run into an issue when trying to integration test a background service that listens to messages from RabbitMq. The issue is, after the first test runs, the subsequent test still has the same registration as the previous one.
I have created a project to illustrate the issue.
Here we set the service for each test to be MessageTest1 and MessageTest2 respectively along with the message that is sent from each test. Yet, as we can see in the Service, while it is receiving the message string "MessageTest2", the service registered is MessageTest1.
The tests use testcontainers to allow for easy running.
Expected Behavior
No response
Steps To Reproduce
https://github.com/Pastafarian/WebApplicationFactoryBugIssue
Exceptions (if any)
No response
.NET Version
8
Anything else?
No response