nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
423 stars 49 forks source link

Configure AutoBogus to generate only positive integers #77

Open nailThrasher opened 2 years ago

nailThrasher commented 2 years ago

Hello! Is there a way to do that (maybe through range set)? If yes, please tell me how, because negative ints are not suitable for my need, and i have a lot of int properties and writing RuleFor each of them will be quite tedious.

nickdodd79 commented 2 years ago

Hey @nailThrasher

Have you tried the override capabilities of AutoBogus? You can define an override for a int and ensure only positive values are generated. You can read about overrides here:

tdYjUSyQ4gCZJk

Hope that helps.

Nick.

304NotModified commented 2 years ago

Another option is RuleForType?

e.g.

var faker = new AutoFaker<Test>();
faker.RuleForType(typeof(int), f => f.Random.Int(0));
faker.RuleForType(typeof(int?), f => f.Random.Int(0));

Or use it with a own class:

public sealed class MyFaker<T> : AutoFaker<T> where T : class
{
    public MyFaker()
    {
        RuleForType(typeof(int), f => f.Random.Int(0));
        RuleForType(typeof(int?), f => f.Random.Int(0));
    }
}

// Usage
var faker = new MyFaker<Test>();