(I didn't find a way to do this, from source code it appears it is not supported, but I might have overlooked it)
I would like to do something akin to:
(new Fixture())
.CreateAnonymous<Circle>(
new CircleSpecification { MinRadius = 1, MaxRadius = 5 }
);
So this is a variation on a similar seed-idiom present in AutoFixture already, but seed idiom is very hard coded (or so I think).
Quiestion: Is it possible to customize a fixture to accept an argument for a specimen?
The best idea I have so far is to build a special Specification class that includes the result object, so that you can do:
public class CircleSpecification {
public double MinRadius { get; set; }
public double MaxRadius { get; set; }
public Circle Circle { get; set; }
}
so that I can register CircleSpecificationSpecimenBuilder
that can be used:
Circle circle = Fixture.CreateAnonymous<CircleSpecification>(
new CircleSpecification { MinRadius = 0.0, MaxRadius = 5.0 }).Circle;
notice that to use CreateAnonymous with seed overload seed argument type has to match method return type.
If you want to assign a value while creating an anonymous instance of Circle
you can use the Build
method:
var fixture = new Fixture();
var c = fixture
.Build<Circle>()
.With(x => x.Radius, 3)
.CreateAnonymous();
However, if there's nothing special about the Radius
property, why not simply assign it a value afterwards?
var fixture = new Fixture();
var c = fixture.CreateAnonymous<Circle>();
c.Radius = 3;
The latter option is much more declarative, and will enable you to use AutoFixture's xUnit.net integration to write a much terser test that removes all the accidental complexity:
[Theory, AutoData]
public void Test3(Circle c)
{
c.Radius = 3;
// Act and assert here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With