Search code examples
.netnunitautofixture

AutoFixture: specify boundaries (or set as "now") for all random dates generated


I have a need to tell AutoFixture that all random dates it generates should be within certain limit, or, for instance, equal to Now. I would like just to set behaviour of Fixture instance so it applies to all use cases throughout the code, that I do not want to examine individually.

How I could configure AutoFixture to do so?


Solution

  • If you would like to return the same DateTime value all over the object graph just inject the desired value to the fixture:

    fixture.Inject(DateTime.Now);
    

    If you need values to be in a specific range though, add the customization like this:

    fixture.Customizations.Add(
        new RandomDateTimeSequenceGenerator(
            minDate: DateTime.Now.AddMinutes(-15),
            maxDate: DateTime.Now.AddMinutes(15)
        ));