How to relate to a stub that has a common parameter itself using Microsoft Fakes?

I use 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 looks like this:

 class RegistryKey : ISecuredItem<RegistryKey> { public SecurityDescriptor Descriptor { get; private set; } public IEnumerable<RegistryKey> Children { get; } } 

I would like to use this interface with Microsoft Fakes and create a stub for it. The problem is that the Fakes form uses StubInterfaceNameHere<> , so in the example above you are trying to do something like StubISecuredItem<StubISecuredItem<StubISecuredItem<StubISecuredItem....

Is it possible? If so, how can I use fakes this way?

+8
c # visual-studio-2012 microsoft-fakes
source share
1 answer

After some experimentation, I found a working solution, although this is not the most elegant.

This is your regular code:

 public interface ISecuredItem<TChildType> where TChildType : ISecuredItem<TChildType> { SecurityDescriptor Descriptor { get; } IEnumerable<TChildType> Children { get; } } 

In the test project, you create the StubImplemtation interface

 public interface StubImplemtation : ISecuredItem<StubImplemtation> { } 

Then in unit test you can do the following:

 var securedItemStub = new StubISecuredItem<StubImplemtation> { ChildrenGet = () => new List<StubImplemtation>(), DescriptorGet = () => new SecurityDescriptor() }; var children = securedItemStub.ChildrenGet(); var descriptor = securedItemStub.DescriptorGet(); 

You can skip all StubImplementation and use RegistryKey if this is not a problem.

+5
source share

All Articles