Library or service name.
Azure.Messaging.ServiceBus
Is your feature request related to a problem? Please describe.
Currently there is no way to stub or mock ServiceBusMessageBatch, making it difficult to test batched calls.
Thank you for your feedback. Tagging and routing to the team members best able to assist.
@drmathias - thanks for raising this issue! Just to make sure I am understanding what you are looking to, are you simply looking to mock out a SendMessages call? Any chance you can include a snippet of the test code you are writing?
I want to be able to test that the data being sent in a message batch is correct. Something like this. Although the return value of the call to CreateMessageBatchAsync(CancellationToken) cannot be mocked.
var clientName = "MyServiceBusClient";
var queueName = "MyServiceBusQueue";
var cancellationToken = new CancellationTokenSource().Token;
var serviceBusMessageBatchMock = new Mock<ServiceBusMessageBatch>();
var serviceBusSenderMock = new Mock<ServiceBusSender>();
serviceBusSenderMock.Setup(callTo => callTo.CreateMessageBatchAsync(cancellationToken))
.Returns(serviceBusMessageBatchMock.Object)
var serviceBusClientMock = new Mock<ServiceBusClient>();
serviceBusClientMock.Setup(callTo => callTo.CreateSender(It.IsAny<string>()))
.Returns(serviceBusSenderMock.Object);
var mockAzureClientFactoryMock = new Mock<IAzureClientFactory<ServiceBusClient>>();
mockAzureClientFactoryMock.Setup(callTo => callTo.CreateClient(clientName))
.Returns(serviceBusClientMock.Object);
var myMessageSender = new MyMessageSender(mockAzureClientFactoryMock.Object);
await myMessageSender.DoSomething(clientName, queueName, cancellationToken);
serviceBusMessageBatchMock.Verify(callTo => callTo.TryAddMessage(It.Is<ServiceBusMessage>(m => m.Body.ToBytes().IsEmpty)),
Times.Exactly(100));
serviceBusSenderMock.Verify(callTo => callTo.SendMessagesAsync(serviceBusMessageBatchMock.Object, cancellationToken),
Times.Once);
@drmathias, our general pattern here would be to make this output type (ServiceBusMessageBatch) creatable using a factory method as opposed to adding a protected constructor, which would make it mockable. You would still be able to create it for your tests, but would not be able to mock it.