bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.66k stars 495 forks source link

DefaultValue for Date #416

Closed garcipat closed 2 years ago

garcipat commented 2 years ago

Hi

I noticed that even with a defined seed, dates are by default changing. This due to the default value of the reference date that is defined as DateTime.Now.

This is a flaw in my eyes since you should get the same value when generating with a seed. Same seed, same value. So I think there should be a static reference date that can be set like the seed as well that applies by default.

so

private const Seed = 123456;

var faker = new Fake<MyClass>()
       .UseSeed(Seed)
       .RuleFor(x => x.Date, f => f.Date.Recent());

var myClass = faker.Generate();

public class MyClass
{
   public DateTime Date{ get; set; }
}

If you run this in a unit test, the Date property will be different every time you run it.

garcipat commented 2 years ago

I have seen that I can overwrite the SystemClock function to get time. Its a little bit hidden bit will do the trick.