question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strugging to unit test code depending on CloudBlockBlob.GetSharedAccessSignature(...)

See original GitHub issue

I noticed via #318 that many methods have been made virtual to allow for mocking. This has been immensely helpful in allowing unit testing of modules depending on this library. However, CloudBlockBlob.GetSharedAccessSignature(...) does not have the virtual qualifier and I’m having difficulty working around this.

I don’t want to depend on the storage emulator for my unit tests, so in essence, anything that depends on a real connection could really do with being made virtual, starting with CloudBlockBlob.GetSharedAccessSignature(...). 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
paulfleming2004commented, May 12, 2017

For reference my mock is now:

new Mock<CloudBlockBlob>(MockBehavior.Loose, 
	new Uri("http://refract.tv/container/file.ext"), 
	new StorageCredentials("fakeaccoutn", Convert.ToBase64String(Encoding.Unicode.GetBytes("fakekeyval")), "fakekeyname"));

My tests that depend on GetSharedAccessSignature now work.

1reaction
blueThunder77commented, Feb 2, 2022

For reference my mock is now:

new Mock<CloudBlockBlob>(MockBehavior.Loose, 
	new Uri("http://refract.tv/container/file.ext"), 
	new StorageCredentials("fakeaccoutn", Convert.ToBase64String(Encoding.Unicode.GetBytes("fakekeyval")), "fakekeyname"));

My tests that depend on GetSharedAccessSignature now work.

Hello, I know this is an old issue, but this solution did not work for me. I would get a NRE when mock CloudBlockBlob with GetSharedAccessSignature method.

Here is the trace:

Message: 
    System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace: 
    CloudBlob.GetCanonicalName(Boolean ignoreSnapshotTime)
    CloudBlob.GetSharedAccessSignature(SharedAccessBlobPolicy policy, SharedAccessBlobHeaders headers, String groupPolicyIdentifier, Nullable`1 protocols, IPAddressOrRange ipAddressOrRange)
    CloudBlob.GetSharedAccessSignature(SharedAccessBlobPolicy policy, SharedAccessBlobHeaders headers, String groupPolicyIdentifier)
    CloudBlob.GetSharedAccessSignature(SharedAccessBlobPolicy policy)

The package version is 9.3.3. It would be nice that we can find out a fix for this.

Thanks!

You’re getting the NullReferenceException because your Mock<CloudBlockBlob> object’s Name property is null. You can fix this using the SetupGet() method, like so:

mockCloudBlockBlob.SetupGet(_ => _.Name).Returns(“testBlob”);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking a CloudBlockBlob and have it return a stream
I'm trying to Moq an Azure CloudBlockBlob and have it return a Stream so that I can test whether my BlobStorage repository is...
Read more >
How to mock or unit test AzureStorageAccount with xunit
Hi, Want to unit test below code: CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connString"); CloudBlobClient blobClient ...
Read more >
Unit testing and mocking with Azure SDK .NET
For the latest guidance on unit testing and mocking, see Unit testing and ... How can you reliably test code with external dependencies?...
Read more >
Introducing Asynchronous Cross-Account Copy Blob
We are excited to introduce some changes to the Copy Blob API with 2012-02-12 version that allows you to copy blobs between storage...
Read more >
Symptom - Windows Azure – Troubleshooting & Debugging
This blog post will describe how to convert your older code based diagnostics configuration to the new WAD 1.2 XML based configuration model....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found