2

I'm trying to generate objects of a class whereby its value should reflect its type.

For example, if its property's type is a string, then that property's value should be "string" and if it is an integer, it should be of a max integer value. So far, this is what I have:

        var personFaker = new AutoFaker<Person>()
            .RuleFor(o => o.Id, 9223372036854775807); //Id is of type long
            .RuleFor(o => o.Name, "string")
            .RuleFor(o => o.Age, 2147483647); //Age is of type integer
        var bookFaker = new AutoFaker<Book>()
            .RuleFor(o => o.Id, 9223372036854775807); //Id is of type long
            .RuleFor(o => o.Title, "string")
            .RuleFor(o => o.Author, "string")
            .RuleFor(o => o.Pages, 2147483647) //Pages is of type integer
....

The problem with this approach is that I would have to list out a .RuleFor for every property of that class. This is tedious and inflexible.

I am wondering if there is a global configuration to specify what values should be generated in AutoFaker or Bogus based on a property's type. For example, for all properties of type string, its generated value can be configured to be set to the word "string".

John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67
  • The "Configuration" Section in the GitHub does not help? https://github.com/nickdodd79/AutoBogus#configuration Why aren't you using the faker at all in `RuleFor`? You can do for example `RuleFor( o => o.Pages, fake => fake.Random.Int() )` – Fildor May 06 '22 at 07:21
  • ^^ The GitHub Page also talks about [Conventions](https://github.com/nickdodd79/AutoBogus#conventions), so you only need to explicitly configure those properties, that aren't filled by convention or differently from what you'd like. – Fildor May 06 '22 at 07:29
  • @Fildor CMIIW, but if I used [Conventions](https://github.com/nickdodd79/AutoBogus#conventions), I would still need to configure every single property, right? At least, that's what I got from that page's example. My objective is to configure every property *type* to a certain value and have AutoBogus auto-populate the objects based on my configuration. I prefer configuring types instead of properties because there's essentially only 3 types (string, long and int) that I need to configure. – John Evans Solachuk May 06 '22 at 08:22

1 Answers1

4

Using just Bogus:

using Bogus;

void Main()
{
   var userFaker = new Faker<User>()
      .RuleForType(typeof(string), f => "this_is_a_string")
      .RuleForType(typeof(int), f => int.MaxValue)
      .RuleFor(u => u.Weight, f => f.Random.Double(100, 200));

   userFaker.Generate(3).Dump();
}

public class User
{
   public string Name;
   public int Age;
   public string Hobby;
   public double Weight;
}

results


You can go further and encapsulate those "default" rules by deriving from Faker<T> like so:

public class MyDefaultFaker<T> : Faker<T> where T : class
{
   public MyDefaultFaker()
   {
      this.RuleForType(typeof(string), f => "default_is_string");
      this.RuleForType(typeof(int), f => int.MaxValue);
   }
}

And your example becomes:

void Main()
{
   var userFaker = new MyDefaultFaker<User>()
      .RuleFor(u => u.Weight, f => f.Random.Double(100, 200));

   var bookFaker = new MyDefaultFaker<Book>();

   userFaker.Generate(3).Dump();
   bookFaker.Generate(3).Dump();
}

results2

Brian Chavez
  • 8,048
  • 5
  • 54
  • 47