I have a method that calls azure API to list blobs from a given container. Then I have some business logic (mostly based on their names) and then this method deletes some of the blobs.
So there is a call to BlobContainerClient.GetBlobs
Now I want to create some unit test, to be sure that my code really do what I want it to do (with xunit and nsubstitute).
While I can mock the call to GetBlobs without any problems (it is declared virtual), what is the best way to build the fake list of BlobItem's?
BlobItem class has an internal constructor, and its Name property is neither abstract or virtual.
So far:
I think this library is not designed for allowing to test easily, I might open a ticket in github
I implemented a hacky solution using reflection, but if we start to have this kind of code all around it is going to be messy
private static T CreateBlobItemStub<T>(string name, params object[] args) { var type = typeof(T); var instance = type.Assembly.CreateInstance( type.FullName, false, BindingFlags.Instance | BindingFlags.NonPublic, null, args, null, null); type.GetProperty("Name").SetMethod.Invoke(instance, new object[] { name }); return (T)instance; }
Maybe there is some library that does it better? if so please can you advise
Any other advice?
Thanks !
CodePudding user response:
A factory exists to create instances designed for mocking purposes: BlobsModelFactory