I’m using Microsoft Fakes in some unit tests I’m working on. My interface looks like this:
interface ISecuredItem<TChildType> where TChildType : class, ISecuredItem<TChildType>
{
SecurityDescriptor Descriptor { get; }
IEnumerable<TChildType> Children { get; }
}
A typical implementation of this looks like:
class RegistryKey : ISecuredItem<RegistryKey>
{
public SecurityDescriptor Descriptor { get; private set; }
public IEnumerable<RegistryKey> Children { get; }
}
I’d like to use this interface with Microsoft Fakes, and have it generate a stub for me. The problem is, the form Fakes uses is StubInterfaceNameHere<>, so in the above example you end up trying to do something like StubISecuredItem<StubISecuredItem<StubISecuredItem<StubISecuredItem....
Is this possible? If so, how do I use Fakes in this way?
After some experimentation I found a working solution although it’s not the most elegant.
This is your regular code:
In your test project you create a StubImplemtation interface
Then in your unit test you can do the following:
You can skip the whole
StubImplementationand useRegistryKeyif that’s no problem.