Answer the question
In order to leave comments, you need to log in
How can you mock this code?
There is the following code:
public class UploadBase
{
protected string userId;
protected string containerName;
protected int maxLengthFile;
protected CloudBlobContainer container;
protected static Logger logger = LogManager.GetLogger("UploadFile");
private static string cacheControl = "public, max-age=7776000";
public UploadBase(string containerName) : this(string.Empty, containerName)
{
maxLengthFile = 21504;
}
public UploadBase(string userId, string containerName)
{
if (string.IsNullOrEmpty(containerName))
new ArgumentNullException(containerName);
this.userId = userId;
this.containerName = containerName;
// Receiving Account storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
// Create Blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// To get conteiner reference
container = blobClient.GetContainerReference(this.containerName);
if (!container.Exists())
{
container.CreateIfNotExists();
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
}
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
Answer the question
In order to leave comments, you need to log in
Obviously, you need to isolate the ConfigurationManager
, wrapping it in a "facade" IConfiguration
with the appropriate access methods, and then mock it.
interface IConfiguration
{
string ConnectionString { get; }
}
class AppSettingsConfiguration: IConfiguration
{
public string ConnectionString
{
get
{
return ConfigurationManager.AppSettings["ConnectionString"];
}
}
}
var config = new Mock<IConfiguration>();
config.Setup(x => x.ConnectionString).Returns("test value");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question