EventHubProducerClient unit testing in c#

Viewed 957

I find that EventHubProducerClient is not unit test friendly (not mockable) in c# and how do you go about testing it, like SendEventsAsync method?

2 Answers

Support for mocking and unit testing is one of the core goals of the new Azure SDKs, and the details of the approach that we use is discussed in the Azure SDK Design Guidelines. All of the public members of EventHubProducerClient are virtual or settable, the class is not sealed, and a parameterless constructor is available to ensure that it is usable with a mocking library such as Moq or FakeItEasy.

You should be able to mock the client using the same approach that you prefer for other types, either by using a mocking framework or inheriting from the class and creating your own mock type.

I'd like to understand a bit more about your statement that the client could not be mocked or tested effectively. Was there a specific scenario or difficulty that you were running into that we may be able to help with?

You can always create your own interface with SendEventsAsync and use it in your classes, which will allow you to easily mock it in unit tests.

Related