Richard Sirny
Richard Sirny

Reputation: 61

Azure C# EventHubClient mock for unit test

I am writing an event publisher for our application which internally uses Azure C# EventHubClient.

I want to unit test that my event gets translated correctly into EventData object (Properties + Body) as well as some other features. Long story short, I need some way to create a mock for EventHubClient. Unfortunately, there does not seem to be a simple way of doing that:

In theory, I could create a wrapper interface and class around the methods I want to use, but it means having more code to maintain. Does anybody know about a better way of unit testing with EventHubClient?

Upvotes: 6

Views: 6028

Answers (1)

kdazzle
kdazzle

Reputation: 4300

I just wrote a simple wrapper around the EventHubClient and mocked that up instead.

public class EventHubService : IEventHubService
{
    private EventHubClient Client { get; set; }

    public void Connect(string connectionString, string entityPath)
    {
        var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString)
            {
                EntityPath = entityPath
            };

        Client =  EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
    }

    public async void Disconnect()
    {
        await Client.CloseAsync();
    }

    public Task SendAsync(EventData eventData)
    {
        return Client.SendAsync(eventData);
    }
}

And then testing is easy: var eventHubService = new Mock<IEventHubService>();

Upvotes: 3

Related Questions