I am trying to find the best approach for using MessageSender in my Azure function as the connection to ServiceBus, in order to unit test it. I have been unable to find the best way to write my code to unit test it.
Code
public static class SomeFunction
{
[FunctionName("SomeFunction")]
public static async Task Run(
[ServiceBusTrigger("someQueue", Connection = ConnectionString)]Message message
, [ServiceBus("someQueue", Connection = ConnectionString")]MessageSender messagesQueue
, MessageReceiver receiver
, string lockToken)
{
...some code
}
}
I tried to change the MessageSender to IMessageSender but got the following binding error
Binding Error
System.InvalidOperationException: "Can't bind ServiceBus to type 'Microsoft.Azure.ServiceBus.Core.IMessageSender."
Then switched it back to the MessageSender and tried mocking it and once I ran my test I got the error below.
Unit Test Example
[TestMethod]
public async Task Run_ValidMessage_Expect_Run_Succesfully()
{
var sbBuilder = new ServiceBusConnectionStringBuilder(ActualSbConnectionString);
Mock<MessageSender>sender = new Mock<MessageSender>(sbBuilder, RetryPolicy.Default);
SomeFunction.Run(_sender.Object);
}
Error:
threw exception:
System.ArgumentException: The argument is null or white space.
Stack Trace:
at MessageSender.ctor(String entityPath, String transferDestinationPath, Nullable`1 entityType, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy)
at MessageSender.ctor(String connectionString, String entityPath, RetryPolicy retryPolicy)
I have been unable to write the code with a working unit test and function. any pointers would be helpful.
Edit: included omitted args