Fake delivery and receipt of a BrokeredMessage Azure ServiceBus

不羁的心 提交于 2019-12-11 07:57:51

问题


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?


回答1:


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.

  • Obvs.AzureServiceBus

  • MessagePropertiesProvider sample




回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/43640911/fake-delivery-and-receipt-of-a-brokeredmessage-azure-servicebus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!