问题
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