Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Set BlobProperties on CloudBlob Object For Testing

I have a Microsoft.Azure.Storage.Blob.CloudBlob blob; object and I am calling the blob.Properties getter to get the blob's BlobProperties object. I want the BlobProperties object so I can read and store the BlobProperties::LastModified property into my own custom model.

I am unable to test this because I cannot construct a BlobProperties object with a non-null LastModified property. I cannot construct the expected object because there are no available constructors or setters to set the LastModified property. I only have a default and copy constructor available:

public sealed class BlobProperties{
    public BlobProperties();

    public BlobProperties(BlobProperties other);

    public DateTimeOffset? LastModified { get; }
}

var props = new Moq.Mock<BlobProperties>() // Error, cannot mock a sealed class
props.Setup(p => p.LastModified).Returns(DateTime.Now); // Unavailable since the class cannot be mocked

// Use the mocked BlobProperties as a return value for a mocked CloudBlockBlob 
var blob = new Moq.Mock<CloudBlockBlob>()
blob.Setup(b => b.Properties).Returns(props.Object);

...

// My custom model adapter
LastModified lastModified = blob.Properties.LastModified;

I am new to C# and I figured this could be solved by mocking with Moq, but the class is marked as sealed so it can't be mocked. Using shims is not an option.

So my question is...

How can I instantiate or override the getter so the LastModified property of a BlobProperties returns some non-null value?

I am using Microsoft.Azure.Storage.Blob, Version=11.2.2.0

See BlobProperties

See CloudBlob

like image 254
E. Pratt Avatar asked Oct 12 '25 10:10

E. Pratt


1 Answers

BlobsModelFactory appears to be the answer. This type has a bunch of static methods with a set of optional parameters to create the BlobProperties instance.

var blobProperties = BlobsModelFactory.BlobProperties(contentType: "application/pdf");
var response = Substitute.For<Azure.Response<BlobProperties>>();
response.Value.Returns(blobProperties);
like image 63
Sam Avatar answered Oct 15 '25 01:10

Sam