2

I have created an instance of a BrokeredMessage and want to test my code around it's deliverycount versus a queue's max delivery count. I don't want to standup a real queue to send and receive the message on, but the deliverycount property is not initialized until the message is delivered. How can I fake this?

Teknos
  • 411
  • 1
  • 7
  • 20

3 Answers3

2

I managed to mock this out and have it work creating a wrapper for complete and abandon message. I was tripping on message.Complete() when unit testing without the wrapper.

Create a Class and Interface to handle the message actions.

    public class MainServiceBusClientWrapper : IServiceBusClientWrapper
    {
        public void Complete(BrokeredMessage message) => message.Complete();
        public void Abandon(BrokeredMessage message) => message.Abandon();
    }

Do this in your class that handles incoming service bus messages:

private IServiceBusClientWrapper _serviceBusWrapper;

_serviceBusWrapper.Complete(message);

Instead of:

message.Complete();

In your unit test you can do something like this to not fail on BrokeredMessage actions:

_serviceBusWrapper.Setup(p => p.Complete(It.IsAny<BrokeredMessage>()));

I hope this helps!

MKT
  • 46
  • 6
0

the deliverycount property is not initialized until the message is delivered. How can I fake this?

BrokeredMessage.DeliveryCount property is read-only, and the value of DeliveryCount will be increased after message is delivered. There is no easy way to mock the BrokeredMessage class, but Obvs.AzureServiceBus integration library seems provide a way to control over properties for the purpose of a particular test, you could try to implement your own mock/fake IMessagePropertiesProvider.

For more detailed information, please check the following links.

Fei Han
  • 26,415
  • 1
  • 30
  • 41
0

I ended up just writing a helper function called GetDeliveryCount that accepted a brokeredmessage as a parameter. This returned message.DeliverCount. For my unit tests, I just mocked this to return whatever I wanted.

Teknos
  • 411
  • 1
  • 7
  • 20