Scooby
Scooby

Reputation: 665

How to unit test HandleEventAsync(ProcessEventArgs args) in Azure.Messaging.EventHub library

We are migrating to Azure.Messaging.EventHubs.Processor v5.6.0 library from Microsoft.Azure.EventHubs.Processor v4.1.0. I m using await messageHandler.HandleEventAsync(args); where args is of ProcessEventArgs type. Most of our business logic that I want to unit test is based on EventData which is a property of ProcessEventArgs. Event data has Readonly dictionary of systemProperties and other properties. The public constructor of EventData does not let us specify the systemProperties or any other property other than EventData body.

Both ProcessEventArgs and EventBody do not have mockable interfaces. In such case can you please advise on how can we unit test the code using ProcessEventArgs?

.NET Version: .Net core app 3.1 NuGet package version : Azure.Messaging.EventHubs.Processor v5.6.0, Microsoft.NET.Test.Sdk v16.6.1

Upvotes: 0

Views: 965

Answers (1)

Jesse Squire
Jesse Squire

Reputation: 7860

The recommended approach for testing your handlers is to call them directly, as you're attempting to do. The constructor for each of the event argument types is public. (example: ProcessEventArgs).

For the PartitionContext, broker-owned properties on EventData, and other model types without a public constructor, the EventHubsModelFactory allows you to create them for use in mocking/testing scenarios.

This article offers more information on the philosophy and approach the Azure SDK takes for supporting unit testing. For Event Hubs specifically, we're currently working on extending our samples to include a set of direct examples for core scenarios such as yours.

Upvotes: 2

Related Questions