Azure IQueueClient: How do you simulate incoming messages?

Viewed 94

The unit I'm testing is an IHostedService which communicates using IQueueClient from Microsoft.Azure.ServiceBus. As far as I can see, there is no function such as .Receive() which is why I am doing this:

_inputQueueClient.Setup(x => x.RegisterMessageHandler(
                It.IsAny<Func<Message, CancellationToken, Task>>(),
                It.IsAny<MessageHandlerOptions>()))
                .Callback<Func<Message, CancellationToken, Task>, MessageHandlerOptions>((x, y) => _messageReceivedHandler = x);

so that I can get the message handling method and call it directly in my unit test. Is this an appropriate way to simulate a message received or is there a better way?

1 Answers

Following on from what gnud said: for a simple unit test you'd just write unit tests against the message handler method and the Azure queue client itself wouldn't be in the picture.

If you can't access that handler easily to unit test (due to it's access level) then this is generally a good indicator of something to break out into a separate class which your original class is dependent on. This helps with your SRP adherence too

Related