¿Cómo se hace referencia a un código auxiliar que tiene un parámetro genérico propio utilizando Microsoft Fakes?

StackOverflow https://stackoverflow.com//questions/12680378

Pregunta

Estoy usando Microsoft Fakes en algunas pruebas unitarias en las que estoy trabajando.Mi interfaz se ve así:

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

Una implementación típica de esto es la siguiente:

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

Me gustaría usar esta interfaz con Microsoft Fakes y que me genere un código auxiliar.El problema es que la forma que usa Fakes es StubInterfaceNameHere<>, entonces en el ejemplo anterior terminas intentando hacer algo como StubISecuredItem<StubISecuredItem<StubISecuredItem<StubISecuredItem....

es posible?Si es así, ¿cómo uso Fakes de esta manera?

¿Fue útil?

Solución

Después de experimentar un poco, encontré una solución que funciona, aunque no es la más elegante.

Este es tu código habitual:

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

En su proyecto de prueba, crea una interfaz StubImplemtation

public interface StubImplemtation : ISecuredItem<StubImplemtation> { }

Luego en tu prueba unitaria puedes hacer lo siguiente:

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

var children = securedItemStub.ChildrenGet();
var descriptor = securedItemStub.DescriptorGet();

Puedes saltarte todo StubImplementation y use RegistryKey si eso no es problema.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top