Create an Unit Test for Azure IoT Hub Function with xUnit and Moq

Muhammad Guruh Ajinugroho 100 Reputation points
2023-02-13T11:51:20.61+00:00

Hi,

I'm new to Azure IoT Hubs and Azure Functions and also new to Unit Testing. Is there any way to do an unit test for azure function using xUnit where I could mock my ServiceClient and RegistryManager objects?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,449 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,128 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
208 questions
{count} votes

Accepted answer
  1. Sander van de Velde 29,271 Reputation points MVP
    2023-02-15T00:40:45.0966667+00:00

    Hello @Muhammad Guruh Ajinugroho ,

    both classes do not implement a base class or interface:

    public class RegistryManager : IDisposable
    
    public class ServiceClient : IDisposable
    

    It works with the Factory method design pattern, not with constructors.

    Thus, writing unit tests will be hard.

    Although still in preview, please check out Microsoft Azure IoT SDKs for .NET v2 Preview Release

    It seems this new V2 version better suites your needs:

    Additionally, all clients can be created using a constructor; factory methods are mostly gone. This enables easier discovery of client initialization as well as unit test mocking.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


0 additional answers

Sort by: Most helpful