How to create a test double for DataLakeDirectoryClient object of Azure.Storage.Files.DataLake c# library?

  Kiến thức lập trình

My code is accessing or precisely downloading the file from Azure’s DataLake Storage Gen2. That file is placed by an upstream system of our application. The files are organized hierarchy of directories.

A typical hierarchy of the file will be:

The functionality of my Subject Under test (GetFiles method) is navigating to correct directory, identify the correct file by a pattern and finally get it as stream.

using Azure.Storage.Files.DataLake;

public class DataLakeService
   //this object is pointing to the directory "sample-container/process_Id_12/"
   private DataLakeDirectoryClient downloadClient;

   private readonly string taskid = "task_id_34";
   public DataLakeService(DataLakeDirectoryClient dataLakeDirectoryClient)
        this.downloadClient = dataLakeDirectoryClient;

   public IAsyncEnumerable<Stream> GetFiles(DateTime processingDate)
       DataLakeDirectoryClient dir = downloadClient.GetSubDirectoryClient(taskid)

           return AsyncEnumerable.Empty<Stream>();
       var pathItems = dir.GetPathsAsync(true);
       await foreach(var pathItem in pathItems)
            string absoluteFullPathToFile = pathItem.Name; //e.g., sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv
            string fileName = System.IO.Path.GetFileName(absoluteFullPathToFile);
            var continer = downloadClient.GetParentFileSystemClient();
            string pathToDirectory = System.IO.Path.GetDirectoryName(absoluteFullPathToFile);
            var directoryClient = container.GetDirectoryClient(pathToDirectory);
            var fileClient = directoryClient.GetFileClient(fileName);
            yield return await fileClient.OpenReadAsync();


My dotnet 6 application is using NUnit 3.12.0 , Moq 4.14.5 frameworks. To test GetFiles method, I have mocked **DataLakeDirectoryClient **dependency using Moq. But it feels like I am deceiving myself with the mock setups for DataLakeDirectoryClient‘s method calls.

The thing I stuck with is to setup a protected method of base class of DataLakeDirectoryClient.

DataLakeDirectoryClient class is derived from DataLakePathClient
That particular protected method of base class is invoked in through a Extenstion method of base class (the extension method GetParentFileSystemClient’s definition)

I tried as below:

using Nunit.Framework;
using Moq;
public class DataLakeService
    public async Task GetFiles_WhenSingleFileInStorage_ThenReturnAStream()
        var mockDirectoryClient = new Mock<DataLakeDirectoryClient>();
              .Setup(dc => dc.GetSubDirectoryClient(It.IsAny<string>()))
              .Setup(dc => dc.Exists(default(CancelationToken)))
              .Returns(Response.FromValue(true, Mock.Of<Response>()));

              .Setup(dc => dc.GetPathsAsync(true, false, default))
              .Returns(() => {
                  var pathItem1 = DataLakeModelFactory
                               false, new DateTime(2024, 7, 10), ETag.All, 256, "", "", "");
                  var pathItem2 = DataLakeModelFactory
                               false, new DateTime(2024, 7, 10), ETag.All, 256, "", "", "");
                  var page1 = Page<PathItem>.FromValues(values: new[] { pathItem1, pathItem2 },
                                       continuationToken: null, response: Mock.Of<Response>());

                  return AsyncPageable<PathItem>.FromPages(new[] { page1 });

         var mockFileSystemClient = new Mock<DataLakeFileSystemClient>();
         mockFileSystemClient.Setup(fsc => fsc.GetDirectoryClient(It.IsAny<string>()))

         var mockFileClient = new Mock<DataLakeFileClient>();
         Stream stream = BinaryData.FromString("This is a line.n This is the second linen").ToStream();
         mockFileClient.Setup(fc => fc.OpenReadAsync(0, default, default, default))
              .Setup(dc => dc.GetFileClient(It.IsAny<string>()))

         // DataLakeDirectoryClient's GetParentFileSystemClient method get invoked in the SUT
         // To set up that method, I have tried both below


         //Error for above is "No protected method DataLakeDirectoryClient.GetParentDataLakeFileSystemClientCore found whose signature is compatible with the provided arguments ()."

              .Setup(dc => dc.GetParentFileSystemClient())
         // Error: "Unsupported expression : dc => dc.GetParentFileSystemClient()
         // Extension meethods(here: SpecializedDataLakeExtensions.GetParentFileSystemClient) May not be used in setup / verification expression "

So How to set up the call to this extension method or protected member of base class ?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website