How do I customize AutoFixture behaviours for specific classes
Asked Answered
T

2

4

I need to enable AutoFixture to create instances of types with circular references (from an API provided by a third party). To do this I can remove the default ThrowingRecursionBehavior as shown below:

public class RecursiveObjectCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Behaviors.OfType<ThrowingRecursionBehavior>()
            .ToList()
            .ForEach(b => fixture.Behaviors.Remove(b));
        fixture.Behaviors.Add(new OmitOnRecursionBehavior());
    }
 }

However, I understand that this will remove the ThrowingRecursionBehavior for all types when the customization is applied via an attribute. How can I limit the modified behavior to only apply to the specific types?

Training answered 6/8, 2015 at 12:31 Comment(0)
H
6

You'll have to create a custom behavior for that.

Here is something to start on:

public class OmitOnRecursionForRequestBehavior : ISpecimenBuilderTransformation
{
    private const int DefaultRecursionDepth = 1;
    private readonly int recursionDepth;
    private readonly object request;

    public OmitOnRecursionForRequestBehavior(object request)
        : this(request, DefaultRecursionDepth)
    {
    }

    public OmitOnRecursionForRequestBehavior(
        object request,
        int recursionDepth)
    {
        if (request == null)
            throw new ArgumentNullException("request");
        if (recursionDepth < 1)
            throw new ArgumentOutOfRangeException(
                "recursionDepth",
                "Recursion depth must be greater than 0.");

        this.recursionDepth = recursionDepth;
        this.request = request;
    }

    public ISpecimenBuilder Transform(ISpecimenBuilder builder)
    {
        if (builder == null)
            throw new ArgumentNullException("builder");

        return new RecursionGuard(
            builder,
            new RecursionForRequestHandler(
                request,
                new OmitOnRecursionHandler(),
                builder),
            recursionDepth);
    }
}

public class RecursionForRequestHandler : IRecursionHandler
{
    private readonly object request;
    private readonly IRecursionHandler handlerForRequest;
    private readonly ISpecimenBuilder handler;

    public RecursionForRequestHandler(
        object request,
        IRecursionHandler handlerForRequest,
        ISpecimenBuilder handler)
    {
        if (request == null)
            throw new ArgumentNullException("request");
        if (handlerForRequest == null)
            throw new ArgumentNullException("handlerForRequest");
        if (handler == null)
            throw new ArgumentNullException("handler");

        this.request = request;
        this.handlerForRequest = handlerForRequest;
        this.handler = handler;
    }

    public object HandleRecursiveRequest(
        object request,
        IEnumerable<object> recordedRequests)
    {
        if (this.request.Equals(request))
            return handlerForRequest.HandleRecursiveRequest(
                request,
                recordedRequests);

        return handler.Create(request, new SpecimenContext(handler));
    }
}

This is how you would use it:

fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(MyType)));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(AnotherType)));

Note that you don't remove the ThrowingRecursionBehavior as it will be used to guard the other requests, otherwise a StackOverflowException will be thrown.

However, if you specify a recursionDepth greater than 1, you'll have to remove the ThrowingRecursionBehavior and create a customized one with a greater or equal recursionDepth.

public class DepthThrowingRecursionBehavior : ISpecimenBuilderTransformation
{
    private readonly int recursionDepth;

    public DepthThrowingRecursionBehavior(int recursionDepth)
    {
        if (recursionDepth < 1)
            throw new ArgumentOutOfRangeException(
                "recursionDepth",
                "Recursion depth must be greater than 0.");

        this.recursionDepth = recursionDepth;
    }

    public ISpecimenBuilder Transform(ISpecimenBuilder builder)
    {
        if (builder == null)
            throw new ArgumentNullException("builder");

        return new RecursionGuard(
            builder,
            new ThrowingRecursionHandler(),
            recursionDepth);
    }
}

fixture.Behaviors.OfType<ThrowingRecursionBehavior>()
    .ToList()
    .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new DepthThrowingRecursionBehavior(2));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(MyType), 2));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(AnotherType), 1));
Heteromerous answered 6/8, 2015 at 14:13 Comment(0)
T
4

Using the answer from Marcio Rinaldi I created a simple solution for the default case where recursion depth is 1.

internal class OmitOnRecursionBehavior<T> : ISpecimenBuilderTransformation
{
    public ISpecimenBuilder Transform(ISpecimenBuilder builder)
    {
        if (builder == null)
            throw new ArgumentNullException("builder");

        return new RecursionGuard(
            builder,
            new RecursionHandler<T>(
                new OmitOnRecursionHandler(),
                builder));
    }
}

internal class RecursionHandler<T> : IRecursionHandler
{
    private readonly IRecursionHandler handlerForRequest;
    private readonly ISpecimenBuilder builder;

    public RecursionHandler(
        IRecursionHandler handlerForRequest,
        ISpecimenBuilder builder)
    {
        if (handlerForRequest == null)
            throw new ArgumentNullException("handlerForRequest");
        if (builder == null)
            throw new ArgumentNullException("builder");

        this.handlerForRequest = handlerForRequest;
        this.builder = builder;
    }

    public object HandleRecursiveRequest(
        object request,
        IEnumerable<object> recordedRequests)
    {
        if (request.Equals(typeof(T)))
            return handlerForRequest.HandleRecursiveRequest(
                request,
                recordedRequests);

        return builder.Create(request, new SpecimenContext(builder));
    }
}

It can be used as follows:

fixture.Behaviors.Add(new OmitOnRecursionBehavior<ClassWithCircularReference>());
Training answered 10/8, 2015 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.