Coder Social home page Coder Social logo

Comments (1)

bchavez avatar bchavez commented on May 31, 2024

If you're using Faker<T> method, then you'll need to extend Faker<T> to capture the internal Faker facade (aka FakerHub) so that setting the seed is flowed to the custom dataset as well.

Ultimately, every Faker facade has an IHasRandomizer interface that exposes the internal SeedNotifier that notifies child datasets when the Faker.Randomizer property has changed.

Basically, what you want to do with a custom dataset is to flow (register) your custom dataset with the internal Faker facade (aka Faker<T>.FakerHub) before using it; so that your custom dataset is notified when a seed value is changed or set.

For example,

void Main()
{
   var id = 0;
   var mealFaker = new ExtendedFaker<Meal>()
      .UseSeed(777)
      .RuleFor(m => m.Id,    f => id++)
      .RuleFor(m => m.Drink, f => f.Food().Drink())
      .RuleFor(m => m.Candy, f => f.Food().Candy());
  
   mealFaker.Generate(5).Dump();
   mealFaker.UseSeed(777); id = 0;
   mealFaker.Generate(5).Dump();
}

public class ExtendedFaker<T> : Faker<T> where T : class
{
   public ExtendedFaker() : base()
   {
      // registers Food() custom dataset with Faker facade (aka FakerHub):
      this.FakerHub.Food();
      // or call
      //ContextHelper.GetOrSet(this.FakerHub, () => new Food());
   }
}

public class Meal
{
   public int Id;
   public string Drink;
   public string Candy;
}

public static class ExtensionsForFood
{
   public static Food Food(this Faker faker)
   {
      return ContextHelper.GetOrSet(faker, () => new Food());
   }
}

public class Food : Bogus.DataSet
{
   private static readonly string[] Candies = { "Hard candy", "Taffy", "Chocolate bar", "Stick candy", "Jelly bean", "Mint", "Cotton candy", "Lollipop" };
   private static readonly string[] Drinks = { "Soda", "Water", "Beer", "Wine", "Coffee", "Lemonade", "Milk" };
   
   public string Candy() => this.Random.ArrayElement(Candies);
   public string Drink() => this.Random.ArrayElement(Drinks);
}

image

And using Faker facade only:

void Main()
{
   var faker = new Faker();
   faker.Food(); // registers Food() custom dataset with faker facade. Or similary call:
                 //ContextHelper.GetOrSet(faker, () => new Food());
   faker.Random = new Randomizer(777);
   Enumerable.Range(1,5).Select(_ => faker.Food().Candy()).Dump();
   faker.Random = new Randomizer(777);
   Enumerable.Range(1,5).Select(_ => faker.Food().Candy()).Dump();
}

image

Hope that helps.

Thanks,
Brian

from bogus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.