How to use AutoBogus/Bogus to generate a constant value for a property based on its type?
Asked Answered
M

1

5

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".

Magdaleno answered 6/5, 2022 at 6:27 Comment(3)
The "Configuration" Section in the GitHub does not help? 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() )Leary
^^ The GitHub Page also talks about Conventions, so you only need to explicitly configure those properties, that aren't filled by convention or differently from what you'd like.Leary
@Leary CMIIW, but if I used 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.Magdaleno
R
6

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

Reredos answered 7/5, 2022 at 0:43 Comment(1)
Thanks! For anyone wondering, you can also do the same thing with AutoBogus by replacing Faker<T> with AutoFaker<T> in the exampleMagdaleno

© 2022 - 2024 — McMap. All rights reserved.