N在 Asp.Net 核心 Xunit 中替代读取/删除文件

问题描述 投票:0回答:0

下面的控制器方法从特定位置读取文件并在完成后将其删除。我需要使用 NSubstitute 模拟文件以读取/删除

    public async Task<AssessmentCustomeDocumentDto> Upload(string fullFileName, long assessmentId)
            {
                
                var tempProfilePicturePath = Path.Combine(_appFolders.SampleProfileImagesFolder, fullFileName);
                double fileSize = 0;
               // Need to read the file here
                using (var fileStream = File.OpenRead(tempProfilePicturePath))
                {
                    var bytes = fileStream.Length;
                    double len = bytes / 1024;
                    fileSize = Math.Round(len, 0);
                    await blockBlob.UploadFromStreamAsync(fileStream);
                }
    
                var fileInfo = new FileInfo(fullFileName);
                var tempFileName = fullFileName;
                var tempFilePath = Path.Combine(_appFolders.SampleProfileImagesFolder, tempFileName);
                File.Delete(tempFilePath);
    
                return Some DTO;
            }

Xunit 测试用例

         [Fact]
         public async Task Should_Upload_Assessment_Custom_Document()
         {
             var _uploadDoc = await _assessmentCutsomDocumentAppService.Upload("xyz.doc", 1001);
         }

在该位置,我没有xyz.doc,需要使用

NSubstiture
模拟文件以便它可以读取
File.OpenRead(tempProfilePicturePath)

c# asp.net-core xunit nsubstitute
© www.soinside.com 2019 - 2024. All rights reserved.