I have a class that consists entirely of string types... I am not happy with this, but cant change it. This class is the representation of some CSVs that are getting parsed.
Now I would like to generate fake instances. For example I would like to generate randomized boolean values and convert them to a string. I have therefor created an implementation of ISpecimenBuilder
which works so far.
public class MyPropertyBuilder : ISpecimenBuilder
{
private readonly Random rand;
public MyPropertyBuilder()
{
this.rand = new Random();
}
public object Create(object request, ISpecimenContext context)
{
var pi = request as PropertyInfo;
if (pi == null)
{
return new NoSpecimen();
}
if (pi.PropertyType == typeof(string) && pi.Name == "MyPropertyName")
{
return (this.rand.NextDouble() > 0.5).ToString();
}
return new NoSpecimen();
}
}
But I somehow don't understand on how to properly use context.Resolve()
and the *Request
classes like RangedNumberRequest()
as in the following code snippet.
public class UnsignedIntegerNumberBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var pi = request as PropertyInfo;
if (pi == null)
{
return new NoSpecimen();
}
if (pi.PropertyType == typeof(string) && pi.Name == "ORDER_NR")
{
return context.Resolve(new RangedNumberRequest(typeof(int), 0, int.MaxValue)).ToString();
}
return new NoSpecimen();
}
}
Of course I can implement my own way to generate a random boolean value and make MyPropertyBuilder
return that, but doesnt that defeat the purpose of AutoFixture as I somehow reinvent the data generation part for some primitve types?
So I guess the question is: How can I properly use AutoFixtures boolean value generator for a specific property?