paint-brush
How to Mock Azure Event Hub in .Net Unit testsby@irynatyshchenko
904 reads
904 reads

How to Mock Azure Event Hub in .Net Unit tests

by Iryna TyshchenkoJuly 24th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

There is no one-size-fits-all solution for EventHub integration in Azure Functions. In this article, we will delve into different approaches for using EventHub in the cloud. We will demonstrate how to effectively test them using NUnit, a popular unit testing framework for NET.
featured image - How to Mock Azure Event Hub in .Net Unit tests
Iryna Tyshchenko HackerNoon profile picture


There is no one-size-fits-all solution for EventHub integration in Azure Functions.

In this article, we will delve into different approaches for using EventHub in Azure Functions and demonstrate how to effectively test them using NUnit, a popular unit testing framework for .NET.


The sample Azure function for each variation will be a simple cloud function with a TimerTrigger. It runs every 5 minutes (specified by the Cron expression “0 */5 * * * *”). The function sends two events (“Event1” and “Event2”) to an Azure Event Hub using an EventHubProducerClient. If any exceptions occur during the event-sending process, they are caught and added to a list.


First approach — using EventHubProducerClient binding.


This function uses an EventHubProducerClient as the output parameter to send events to the Azure Event Hub.


AzureEventHubFunction1 code



Second approach — using IAsyncCollector<EventData> parameter.

This function uses IAsyncCollector<EventData> as the output parameter to collect and send events to the Azure Event Hub.


AzureEventHubFunction2 code


Third approach — using custom EventHubService implementation.

The third implementation follows the dependency injection pattern. It takes an IEventHubService<string> as a constructor parameter, allowing the injection of an external service to handle Event Hub-related operations.



AzureEventHubFunction3 code


And EventHubServiceimplementation is below:

EventHubservice implementation



This class encapsulates the event-sending functionality for Azure Event Hubs, making it reusable and easy to manage for different types of messages. In general, it is beneficial to apply for complex use cases with custom logic.


How to configure the event hub for Azure functions and how to use different bindings can be found here.


Unit tests

  1. EventHubProducerClient provides a mock-friendly design as all its public members are virtual or settable. The class is not sealed and offers a parameterless constructor, making it compatible with popular mocking libraries like Moq or FakeItEasy.


Thus, we can use a mocking library to set the behavior of SendAsync function. Then we can compare the EventData using the extensions method.



AzureEventHubFunction1 unit test


Extension method mentioned above: It will return output in deserialized format from the event body.


  1. To mock IAsyncCollector<EventData> we will be using a custom implementation AsyncCollector<EventData> that captures the events sent by the function.


Then, we will be able to verify that the events captured in the AsyncCollector match the expected events, effectively allowing the test to check the correct behavior of the Azure Function without actually sending events to the Event Hub during testing.



AzureEventHubFunction2 unit test



Async data collector mocked the implementation


AsyncCollector implementation for unit tests



  1. This test implementation uses a stub of IEventHubService<T> to mock the Event Hub service’s behavior and capture the messages sent during the test. By using this approach, the test verifies the correct behavior of AzureEventHubFunction3 without interacting with the actual Event Hub, ensuring the test’s isolation and reliability.



AzureEventHubFunction3 unit test



EventHubServiceStubimplementation


Implementation of EventHubService for unit tests



That's all. The source code for the described approaches can be found in the GitHub repo: https://github.com/FairyFox5700/AzureEventHubProcessorUnitTests.


Cheers 😉 🍷.


The lead image for this article was generated by HackerNoon's AI Image Generator via the prompt "a computer screen displaying lines of code".