-1
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?

Cuteqn124
  • 3
  • 3
  • It's virtual, so you can just mock it. It does have a few default parameters though, but that too is documented all over the web. Where exactly are you stuck? Does your intended code not compile, not run, not return what you expect? Read [ask] and properly explain where you're stuck and what you have tried. – CodeCaster Jan 10 '21 at 14:00
  • Hi @CodeCaster, I am getting "an expression tree may not contain a call or invocation that uses optional arguments". After specifying the default parameters to the download method, now I am getting "argument 1: cannot convert from azure.storage.files.shares.models.sharefiledownloadinfo to azure.response" error. Any help is appreciated. – Cuteqn124 Jan 10 '21 at 16:33

1 Answers1

0

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);
    //...
}
Cuteqn124
  • 3
  • 3