Search code examples
c#asp.net-corenunitazure-data-lake

How to Moq DataLakeServiceClient in .Net core


I have a .NET Core application, in which I have a method that will connect to Azure Data Lake:

public DataLakeServiceClient GetDataLakeServiceClient(string StorageAccount)
{
    string clientId = _authenticationConfig.ClientId.Substring(6, _authenticationConfig.ClientId.Length - 6);
    TokenCredential credential = new ClientSecretCredential(
        _authenticationConfig.TenantId, clientId, _authenticationConfig.ClientSecret, new TokenCredentialOptions());

    string dfsUri = "https://" + StorageAccount + ".dfs.core.windows.net";

    DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(new Uri(dfsUri), credential);
    return dataLakeServiceClient;
}

This is how I use it:

DataLakeServiceClient dataLakeServiceClient = _azureDataFactoryRepository.GetDataLakeServiceClient(_azureStorageClient.AzureStorageAccount02Value);

I try to write a unit test case where I try to moq the GetDataLakeServiceClient:

_azureDataFactoryRepository.Setup(x => x.GetDataLakeServiceClient(It.IsAny<string>())).Returns();

In the above code it should return DataLakeServiceClient but I am struggling how can I do this.

I do not see any method IDataLakeServiceClient also.

Can someone help me to setup above method?


Solution

  • DataLakeServiceClient is an abstract class so you can just mock this and inject this mock into the higher level mock. So you want something like:

    var moqDataLakeServiceClient = new Mock<DataLakeServiceClient>();
    _azureDataFactoryRepository
                 .Setup(x => x.GetDataLakeServiceClient(It.IsAny<string>()))
                .Returns(moqDataLakeServiceClient.Object);
    

    I'm not sure what mocking client you're using so the above is using Moq.