使用回调模拟的 Azure CloudFile DownloadToStream 方法不起作用 [英] Azure CloudFile DownloadToStream method mocking with Callback not working

查看:24
本文介绍了使用回调模拟的 Azure CloudFile DownloadToStream 方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从azure文件存储中读取文件并处理数据的代码.我正在使用最新的文件存储 nuget 包.

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();

为了对这部分进行单元测试,我试图在单元测试中模拟这样的 DownloadToStream 方法.

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).

推荐答案

正如 Pavel 在评论中提到的,我缺少其他参数.我假设不需要可选参数.这解决了问题.

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;
            });

这篇关于使用回调模拟的 Azure CloudFile DownloadToStream 方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆