Search code examples
c#unit-testingmockingazure-storage

How to mock ShareFileClient.Download() method? The call to download method returns null


public void FileDownloadFromAzure(String fileWithPath)
{
    char separator = '\\';
    String localRootDir = "C:\\XYZ";
    String azureRootDir = "XYZ";
    String fileName = fileWithPath.Substring(fileWithPath.LastIndexOf(separator) + 1);
    String dirName = fileWithPath.Substring(0, fileWithPath.LastIndexOf(separator));
    String destDirWithFullPath = localRootDir + "\\" + dirName + "\\" + fileName;
    String sourceDirWithFullPath = azureRootDir + "\\" + dirName;
    try
    {
        ShareDirectoryClient directory = m_ShareClient.GetDirectoryClient(sourceDirWithFullPath);
        ShareFileClient file = directory.GetFileClient(fileName);

        // Download the file
        ShareFileDownloadInfo download = file.Download();//call returns null
        using (Stream stream = m_FileSystem.File.OpenWrite(destDirWithFullPath))
        {
            download.Content.CopyTo(stream);
        }
    }
    catch(Exception ex) {}
}

FileDownloadFromAzure above works as expected.

I am trying to write unit test for the same.

[TestMethod]
public void FileDownloadFromAzure_ValidCall()
{
    //arrange
    var shareDirectoryClient = new Mock<ShareDirectoryClient>();
    var shareFileClient = new Mock<ShareFileClient>();
    var shareFileDownloadInfo = new Mock<Response<ShareFileDownloadInfo>>();
    shareFileClient.Setup(s => s.Download(It.IsAny<HttpRange>(), 
                                              false, null,
                                              It.IsAny<CancellationToken>()))
                                    .Returns(shareFileDownloadInfo.Object);
    shareDirectoryClient.Setup(s => s.GetFileClient(It.IsAny<String>())).Returns(shareFileClient.Object);
    _shareClient.Setup(s => s.GetDirectoryClient(It.IsAny<String>())).Returns(shareDirectoryClient.Object);

    var sut = new FileSystemIOProcessor(_shareClient.Object,
                                        _fileSystem.Object);
    //act
    String fileWithPath = "TEST\\TEST.TXT";
    var actual = sut.FileDownloadFromAzure(fileWithPath);

    //assert
    Assert.AreEqual(true, actual);
}

But I am new to using mock. Could you please share your thoughts on mocking ShareFileClient.Download() method?


Solution

  • Blockquote I have resolved the issue by taking a clue from the following post. Mocking File.OpenWrite()

    String const TEMP_DIR_NAME = "TEMPDIR";
    String const TEMP_FILE_NAME = "TEMPFILE.TXT";
    
    [TestInitialize]
    public void TestInitialize()
    {
        //...Create temp dir and temp file as part of TestInitialize()
    }
    
    [TestMethod]
    public void FileCopyFromAzureToLocal()
    {
    ...
    //Arrange
        String sourceDirWithFullPath = azureRootDir + "\\" + TEMP_DIR_NAME;
        ShareDirectoryClient directory = shareClient.GetDirectoryClient(sourceDirWithFullPath);
        ShareFileClient file = directory.GetFileClient(TEMP_FILE_NAME);
        shareFileDownloadInfo = file.Download();
        shareFileClient.Setup(s => s.Download(It.IsAny<HttpRange>(),
                                              false, null,
                                              It.IsAny<CancellationToken>()))
                                              .Returns(shareFileDownloadInfo);
    ...
    //Act
    //Assert    
    }
    
    [ClassCleanup]
    public static void Class_Cleanup()
    {
        //...
        DeleteDirectoryOnAzure(TEMP_DIR_NAME, recursive=true);
        //...
    }