Pre-populate an object with fake data

Viewed 2076

Is there a way to pre-populate an object with fake data with Faker without specifying property rules explicitly. I found it is taking way too much time specifying rules for each property in order to populate an object with random data. How about a method that would automatically do that for me, based on property data type. It is so easy to achieve that with NBuilder, unfortunately not supporting .Net Core.

Here is the code I am using to populate CategoryViewModel:

var category = PopulateCategoryViewModelFaker().Generate();

    private Faker<CategoryViewModel> PopulateCategoryViewModelFaker()
    {
        var faker = new Faker<CategoryViewModel>();
        //faker.StrictMode(true);
        faker.RuleFor(x => x.DateCreated, f => DateTime.Now);
        faker.RuleFor(x => x.Description, f => f.Lorem.Sentence(2));
        faker.RuleFor(x => x.ParentCategory, f => null);
        faker.RuleFor(x => x.Status, f => null);
        faker.RuleFor(x => x.Title, f => f.Lorem.Sentence(2));
        faker.RuleFor(x => x.CategoryId, f => Guid.NewGuid());
        faker.RuleFor(x => x.CreatedByUser, f => null);
        faker.RuleFor(x => x.CrumbPath, f => null);
        faker.RuleFor(x => x.StatusId, _ctx.CategoryStatuses.First().StatusId);
        faker.RuleFor(x => x.CreatedByUserId, x => Guid.NewGuid());

        return faker;
    }

I would be looking something like:

        var cat = new CategoryViewModel();
        cat  = new Faker<CategoryViewModel>().Generate();

The above creates an object, however with no fake data.

1 Answers
Related