Coder Social home page Coder Social logo

Comments (2)

MihaiDaniel avatar MihaiDaniel commented on May 29, 2024 1

@bchavez Indeed that was the issue, thanks for the quick response!

from bogus.

bchavez avatar bchavez commented on May 29, 2024

The problem is the declaration .RuleFor for the following inner class as a constant:

.RuleFor(mc => mc.MyInnerClass, new MyInnerClassFaker().Get());

The correct way to invoke the lambda upon each generaiton is to use the lambda overload; not the value/constant overload:

.RuleFor(mc => mc.MyInnerClass, f => new MyInnerClassFaker().Get());
                               ^^^^^^

You need to make sure if you want something done on each generation instance, that you use lambdas .RuleFor( ... , f => f); not .RuleFor( ... , valueConstant).

And a working example:

void Main()
{
   MyClassFaker myClassFaker = new();
   MyClass class1 = myClassFaker.Get();
   MyClass class2 = myClassFaker.Get();
   class1.MyInnerClass.Id.Dump();
   class2.MyInnerClass.Id.Dump();
}

public class MyClass
{
   public int Id { get; set; }

   public MyInnerClass MyInnerClass { get; set; }
}

public class MyInnerClass
{
   public int Id { get; set; }
}

public class MyClassFaker
{
   private Bogus.Faker<MyClass> faker = new();

   public MyClassFaker()
   {
      faker
         .RuleFor(mc => mc.Id,           f => f.Random.Int())
         .RuleFor(mc => mc.MyInnerClass, f => new MyInnerClassFaker().Get()); // Your bug was here.
   }

   public MyClass Get() { return faker.Generate(); }
}

public class MyInnerClassFaker
{
   private Bogus.Faker<MyInnerClass> faker = new();

   public MyInnerClassFaker()
   {
      faker.RuleFor(mc => mc.Id, f => f.Random.Int());
   }

   public MyInnerClass Get() { return faker.Generate(); }
}

OUTPUT

1449290238
130403538

Hope that helps.

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.