-1

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?

Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89

1 Answers1

0

If I understand correctly all properties of the class are of type string, but represent other data types (boolean, integer or dates).

My suggestion would be to create a FixtureBuilder class with property types you expects to be. With Autfixture you will be able to generate random values and then convert it to the csv representation.

public class CsvData
{
    public string Enabled { get; set; }
    public string Quantity { get; set; }
    public string Price { get; set; }
}

public class CsvDataBuilder
{
    public bool Enabled { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; } 

    public CsvData ToCsvData()
    {
        return new CsvData
        {
            Enabled = Enabled.ToString(),
            Quantity = Quantity.ToString(),
            Price = Price.ToString()
        };
    }
}

Then in the tests you can create builder within

var dataFromCsv = new Fixture().Create<CsvDataBuilder>().ToCsvData();
Fabio
  • 31,528
  • 4
  • 33
  • 72