Deryn Johnstone

Deryn Johnstone

  • NA
  • 35
  • 1.3k

How to write a functional test for Azure File Storage file upload

Nov 14 2022 1:47 AM

I am new to testing and wondering how to write a functional test for uploading a file to Azure File Storage.

        public async Task<ReturnObject> UploadFile(AzureObjectsResource azureObjectsResource)
        {
            var response = new ReturnObject();

            AzureObjectsHelper.ValidateObjects(azureObjectsResource, response);

            if (response.Success)
            {
                try
                {
                    object azureReturnResponse;

                    using (var stream = new MemoryStream(azureObjectsResource.FileContent))
                    {
                        object azureResponse = await azureObjectsResource.ShareFileClient.UploadAsync(stream);

                        var azureResponseObject = (Response<ShareFileUploadInfo>)azureResponse;
                        azureReturnResponse = azureResponseObject.Value;

                        stream.Close();
                    }

                    response.AzureResponseObject = azureReturnResponse;
                }
                catch (Exception ex)
                {
                    response = new ReturnObject
                    {
                        Success = false,
                        ErrorMessage = $"An exception was hit trying to upload the file. Message: {ex.Message}; Stack Trace: {ex.StackTrace}",
                        AzureResponseObject = null
                    };
                }
            }

            return response;
        }

 


Answers (1)