nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
431 stars 50 forks source link

Property value is filed with random data even when RuleFor is defined #35

Closed wxtsxt closed 4 years ago

wxtsxt commented 4 years ago

I have a class that has a LocalDate? field (NodaTime library).

new AutoFaker<MyClass>()
.RuleFor(x=>x.LocalDate, new LocalDate(2000,1,1))
.Generate();

does not generate anything.

LocalDate field is populated with random data first and throws an exception. But when I use Faker instead of AutoFaker everything is OK and I get a proper generated class.

To illustrate run 2 tests below. Both should succeed.

public sealed class TimeHolder
  {
    private DateTime _time;
    private bool _unstable;

    public DateTime Time
    {
      get => _time;
      set
      {
        if (value.Day != 2 && value.Month != 2)
          _unstable = true;
        if (_unstable)
          throw new Exception();

        _time = value;
      }
    }
  }

  public class TestValidValueCreation
  {
    private readonly DateTime _validDate = new DateTime(2020, 2, 2);

    [Fact]
    public void TestFaker()
    {
      var created = new Faker<TimeHolder>()
        .RuleFor(x => x.Time, _validDate)
        .Generate();
    }

    [Fact]
    public void TestAutoFaker()
    {
      var created = new AutoFaker<TimeHolder>()
        .RuleFor(x => x.Time, _validDate)
        .Generate();
    }
  }
nickdodd79 commented 4 years ago

Hey @wxtsxt

This is a great find, thanks. Turns out the RuleFor definitions weren't being skipped during the creation of an object. All good now and will be released shortly in the next version.

Nick.

nickdodd79 commented 4 years ago

@wxtsxt, version 2.8.2 has been released.

wxtsxt commented 4 years ago

Works on my side. Thanks.