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));