Mock BlobResultSegment
See original GitHub issueIn #335 this class is not sealed anymore, but I still have a hard time mocking this class with Moq as the constructor is internal and results in an exception.
System.NotSupportedException : Parent does not have a default constructor. The default constructor must be explicitly defined.
I tried to go with reflection, but fieldInfo is always null.
var segment = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(BlobResultSegment));
var fieldInfo = typeof(BlobResultSegment).GetField(@"Results");
I even tried to GetFields with a lot BindingFlags, but then I only got the private backing fields and not the public fields.
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly;
The goal to unseal the class was to be able to mock it. What is the best solution to mock this class?
My not so pretty work around is as follows:
private BlobResultSegment CreateResultSegment(CloudBlockBlob blob)
{
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly;
var segment = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(BlobResultSegment));
var fields = segment.GetType().GetFields(flags);
var fieldInfo = fields.First(f => f.Name.Contains(@"Results"));
fieldInfo.SetValue(segment, new[] { blob });
return (BlobResultSegment)segment;
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
NSubstitute or moq ListBlobsSegmentedAsync and its ...
I am trying to create the unit test case for the below method. I tried to mock the container, ListBlobsSegmentedAsync but the response....
Read more >Azure Blob Storage Part 2 - Blob Gallery - Mitch Valenta
Finishing off this guide with part 2, we will be covering how to get a list of blobs, basics of blob access, and...
Read more >Mock CloudBlobClient with AutoFac and AutoMock
Coding example for the question Mock CloudBlobClient with AutoFac and AutoMock. ... Returns(ci => new BlobResultSegment(items.ToArray(), null)); container.
Read more >MockStorageBlobManagement.cs
searchcode is a free source code search engine. Code snippets and open source (free software) repositories are indexed and searchable.
Read more >Process large-scale datasets by using Data Factory and ...
Describes how to process huge amounts of data in an Azure Data Factory pipeline by using the parallel processing capability of Azure Batch....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @tverboon, @PureKrome
Thanks for sharing the information and the workaround for the issue (which seems to be the only working one).
As mentioned in #465, we are discussing improvements to increase mock-ability of our library(by either virtualizing the properties and adding default constructors or adding interfaces for the types). These changes once pinned down, will be included in our next breaking change release(9.0) and we will announce the final decision on this. Shall we use #465 to track this task and close the related ones such as this issue?
Thanks, Elham
@tverboon out of interest, can you provide some code to how you use a
BobResultSegment
in some normal code (not mocks, etc)I would like to see how it’s used, to then understand how it could be mocked (regardless if the mocking doesn’t work, currently). This way, we can help show the MS Team how we are using their code and suggest how/what should be fixed.
Example reference to another Issue re: mocked => https://github.com/Azure/azure-storage-net/issues/465