AutoFixture: Pass Argument to Sample Maker

(I did not find a way to do this, from the source code it seems that it is not supported, but I could have missed it)

I would like to do something similar to:

(new Fixture())
    .CreateAnonymous<Circle>(
        new CircleSpecification { MinRadius = 1, MaxRadius = 5 }
    );

So, this is a variant of the similar seed idiom present in AutoFixture, but the seed idiom is very hardcoded (or so I think).

Quiestion . Is it possible to configure the device to accept an argument for a sample?

The best idea I have so far is to create a special Specification class that includes a result object so you can:

public class CircleSpecification {
    public double MinRadius { get; set; }
    public double MaxRadius { get; set; }

    public Circle Circle { get; set; }
}

so that I can register CircleSpecificationSpecimenBuilderwhich can be used:

Circle circle = Fixture.CreateAnonymous<CircleSpecification>(
    new CircleSpecification { MinRadius = 0.0, MaxRadius = 5.0 }).Circle;

note that to use CreateAnonymous with the semantic overload type, the seed must match the type of the returned method.

+5
1

Circle, Build:

var fixture = new Fixture();
var c = fixture
    .Build<Circle>()
    .With(x => x.Radius, 3)
    .CreateAnonymous();

, Radius, ?

var fixture = new Fixture();
var c = fixture.CreateAnonymous<Circle>();
c.Radius = 3;

AutoFixture xUnit.net , , :

[Theory, AutoData]
public void Test3(Circle c)
{
    c.Radius = 3;

    // Act and assert here
}
+5

All Articles