使用 NSubstitute 和 Xunit asp.net core 模拟 Azure 云存储,为 GetBlockBlobReference 获取 NULL

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

我有下面的方法,需要模拟

CloudBlobContainer

public async static Task<CloudBlobContainer> GetCloudBlobContainer(string containerName, IConfiguration configuration, bool isPrivate = true)
        {
            string storageConnectionString = configuration[AzureStorageConnectionStringKey];
            CloudBlobContainer container = null;
            containerName = containerName.ToLower();
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            var blobClient = storageAccount.CreateCloudBlobClient();
            container = blobClient.GetContainerReference(containerName); --> This is getting NULL
            await container.CreateIfNotExistsAsync();

            if (isPrivate)
            {
                await container.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Off
                });
            }
            else
            {
                await container.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            return container;
        }

实现方法之一

GetCloudBlobContainer

public async Task<AssessmentCustomeDocumentDto> Upload(string fullFileName, long assessmentId)
        {
            var tenancyName = (await GetCurrentTenantAsync()).TenancyName;
            var blobContainer = await _azureStorageHelper.GetCloudBlobContainer(tenancyName);
            string fileName = Path.GetFileNameWithoutExtension(fullFileName) + "-" + Guid.NewGuid().ToString() + Path.GetExtension(fullFileName);
            var blockBlob = blobContainer.GetBlockBlobReference(fileName);
            var tempProfilePicturePath = Path.Combine(_appFolders.SampleProfileImagesFolder, fullFileName);
            double fileSize = 0;
            using (var fileStream = File.OpenRead(tempProfilePicturePath))
            {
                var bytes = fileStream.Length;
                double len = bytes / 1024;
                fileSize = Math.Round(len, 0);
                await blockBlob.UploadFromStreamAsync(fileStream);
            }

            //// Other logics
            return new AssessmentCustomeDocumentDto();
        }

var blockBlob = blobContainer.GetBlockBlobReference(fileName);
为 NULL

Xunit 测试

[Fact]
         public async Task Should_Upload_Assessment_Custom_Document()
         {
             var container = Substitute.For<CloudBlobContainer>(new Uri("http://legalregtechquestiondocs/"));
             // Other logics
             UsingDbContext(async
                 context =>
             {
                // Other logics
                 _azureStorageHelper.GetCloudBlobContainer("Tenant").Returns(container);

             });
             var _uploadDoc = await _assessmentCutsomDocumentAppService.Upload("xyz.doc", 1001);
             SaveChanges();
             // assert
             Assert.NotNull(_uploadDoc);
             Assert.Equal("xyz.doc",_uploadDoc.DisplayFileName);
         }

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