没有正确地控制C#Stream,导致EndOfStream第一次为真

没有正确地控制C#Stream,导致EndOfStream第一次为真,c#,csv,mocking,C#,Csv,Mocking,在我的测试案例中: Mock<HttpPostedFileBase> uploadedFile = new Mock<HttpPostedFileBase>(); uploadedFile .Setup(f => f.InputStream) .Returns(fileStream); uploadedFile .Set

在我的测试案例中:

Mock<HttpPostedFileBase> uploadedFile = new Mock<HttpPostedFileBase>();

            uploadedFile
                .Setup(f => f.InputStream)
                .Returns(fileStream);

            uploadedFile
                .Setup(f => f.InputStream.Position)
                .Returns(741);

            uploadedFile
                .Setup(f => f.InputStream.CanRead)
                .Returns(true);

            uploadedFile
                .Setup(f => f.FileName)
                .Returns(csvFilePath);

            uploadedFile
                .Setup(f => f.ContentLength)
                .Returns(contentLength);

            uploadedFile
                .Setup(f => f.InputStream.Length)
                .Returns(contentLength);

            var result = await _assetController.UploadData(assetId, uploadedFile.Object);
Mock uploadedFile=new Mock();
上载文件
.Setup(f=>f.InputStream)
.Returns(fileStream);
上载文件
.Setup(f=>f.InputStream.Position)
.申报表(741份);
上载文件
.Setup(f=>f.InputStream.CanRead)
.返回(真);
上载文件
.Setup(f=>f.FileName)
.Returns(csvFilePath);
上载文件
.Setup(f=>f.ContentLength)
.Returns(contentLength);
上载文件
.Setup(f=>f.InputStream.Length)
.Returns(contentLength);
var result=await\u assetController.UploadData(assetId,uploadedFile.Object);
其中csvFilePath指向合法的CSV文件

我怀疑我没有正确地吸烟,因为以下情况:

public static async Task<string> ReadCsvFileAsync(Stream fileStream)
        {
            try
            {
                string fileContents = String.Empty;
                fileStream.Position = 0;
                using (StreamReader streamReader = new StreamReader(fileStream))
                {

                    while (!streamReader.EndOfStream)
                    {
                        fileContents = await streamReader.ReadToEndAsync();
                    }
                } 
    //.. snip
公共静态异步任务ReadCsvFileAsync(流文件流)
{
尝试
{
string fileContents=string.Empty;
fileStream.Position=0;
使用(StreamReader StreamReader=newstreamreader(fileStream))
{
而(!streamReader.EndOfStream)
{
fileContents=等待streamReader.ReadToEndAsync();
}
} 
//…剪断
每当我试图处理上传的CSV文件时,第一次
EndOfStream
标志总是正确的


这个帖子在某种程度上描述了我的困境,但正如我所说的,我认为我可能没有正确地设置Moq ing(可能我缺少一个关键的
。设置(f=>f.)

使用实际的流来伪造可用的数据

//Arrange
HttpPostedFileBase httpPostedFile = Mock.Of<HttpPostedFileBase>();
var mock = Mock.Get(httpPostedFile);
mock.Setup(_ => _.FileName).Returns(csvFilePath);
var memoryStream = new MemoryStream();
//...TODO: populate fake stream with data

//setup mock to return stream
mock.Setup(_ => _.InputStream).Returns(memoryStream);
mock.Setup(_ => _.ContentLength).Returns(memoryStream.Length);

//...setup any other desired behavior

//Act
var result = await _assetController.UploadData(assetId, httpPostedFile);

//Assert
//...Assert code here
//排列
HttpPostedFileBase httpPostedFile=Mock.Of();
var mock=mock.Get(httpPostedFile);
mock.Setup(=>u.FileName).Returns(csvFilePath);
var memoryStream=新的memoryStream();
//…TODO:用数据填充假流
//设置模拟以返回流
mock.Setup(=>InputStream).Returns(memoryStream);
mock.Setup(=>x0.ContentLength).Returns(memoryStream.Length);
//…设置任何其他所需的行为
//表演
var result=await\u assetController.UploadData(assetId,httpPostedFile);
//断言
//…在此断言代码
谢谢。合并有助于完成我需要的代码。