Azure CloudFile DownloadToStream method mocking with Callback not working

北城余情 提交于 2021-02-11 12:33:33

问题


This is the code for reading file from azure file storage and process the data. I am using latest file storage nuget packages.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("FileStorageConnectionString");
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare cloudShare = fileClient.GetShareReference("FileShareName");

    var cloudFile = this.cloudShare.GetRootDirectoryReference().GetFileReference("file.txt");    
    var memoryStream = new MemoryStream();

        cloudFile.DownloadToStream(memoryStream);
        memoryStream.Position = 0;

        var data = ProcessData(new StreamReader(memoryStream));
        memoryStream.Dispose();

To unit test this part I am trying to mock DownloadToStream method like this in a unit test.

var stream = new MemoryStream();

        var fileStream = File.OpenRead("file.txt");
        fileStream.CopyTo(stream);
        stream.Position = 0;
       var cloudFile = new Mock<CloudFile>(fakeStorageUri, fakeStorageCredentials);
        cloudFile.Setup(x => x.DownloadToStream(It.IsAny<Stream>(), null, null, null))
            .Callback((Stream target) =>
            {
                stream.CopyTo(target);
                target.Position = 0;
            });

but I am getting this exception while executing the unit test. What am I doing wrong here?

 Invalid callback. Setup on method with parameters (Stream,AccessCondition,FileRequestOptions,OperationContext)
 cannot invoke callback with parameters (Stream).

回答1:


As Pavel mentioned in the comments, I am missing other parameters. I assumed no need of optional parameters. This solved the issue.

.Callback((Stream target, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext) =>
            {
                stream.CopyTo(target);
                target.Position = 0;
            });


来源:https://stackoverflow.com/questions/61498758/azure-cloudfile-downloadtostream-method-mocking-with-callback-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!