Skip to content

"adding" onto a rule #552

Closed Answered by bchavez
mlapaglia asked this question in Q&A
Discussion options

You must be logged in to vote

Not exactly the most elegant, but here's one way to apply .OrNull/.OrDefault to all rules stored in a Faker<T> object:

void Main()
{
   var faker = new ChaosFaker<User>()
      .RuleFor(u => u.FirstName, f => f.Name.FirstName())
      .RuleFor(u => u.LastName, f => f.Name.LastName())
      .RuleFor(u => u.Age, f => f.Random.Number(18, 27))
      as ChaosFaker<User>;
   
   faker.Generate(5).Dump();
   
   faker.WithChaos();
   
   faker.Generate(5).Dump();
}

public class User
{
   public string FirstName;
   public string LastName;
   public int? Age;
}

public class ChaosFaker<T> : Faker<T> where T : class{
   
   public ChaosFaker<T> WithChaos()
   {
      var allRulesets = this.Actions.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by bchavez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants