Skip to content

v4.3.0 - New features and fixes

Compare
Choose a tag to compare
@DyegoMaas DyegoMaas released this 12 Feb 23:07
· 65 commits to main since this release

Features

This release adds some new features and improves utility for common usage scenarios.

DateTimes are now supported in FillWithEmptyValuesBehavior

DateTimes properties will be set to 1753/1/1. This value can be configured through the new option StartDate.

DateTimes are now supported in FillWithSequentialValuesBehavior

DateTimes properties will be set to 1753/1/1. This value can be configured through the new option StartDate.
By default, dates will be increment on a day-by-day basis. The increments can be configured through the new option DateTimeIncrements to one of these:

  • Days
  • Months
  • Years
  • Hours
  • Minutes
  • Seconds
  • Milliseconds
  • Ticks

Example configuration

var behavior = new FillWithSequentialValuesBehavior(options =>
{
    options.DateTimeOptions = new DateTimeSequenceOptions
    {
        StartDate = new Date(1995, 2, 1),
        DateTimeIncrements = DateTimeIncrements.Days
    };
});

var customers = MagicFactory.For<Customer>()
  .WithBehavior(behavior)
  .Many(3)
  .Build()
  .ToList();

customers[0].Birthday.Should().Be(1.February(1995));
customers[1].Birthday.Should().Be(2.February(1995));
customers[2].Birthday.Should().Be(3.February(1995));

Adds support to filling nullable fields and properties

Previously, nullable properties and fields were ignored. Now they are filled by default, in addition to normal fields and properties:

public class Class {
    public string? NullableStringProperty { get; set; }
    public byte? NullableByteProperty { get; set; }
    public short? NullableShortProperty { get; set; }
    public ushort? NullableUShortProperty { get; set; }
    public int? NullableIntProperty { get; set; }
    public uint? NullableUIntProperty { get; set; }
    public long? NullableLongProperty { get; set; }
    public ulong? NullableULongProperty { get; set; }
    public float? NullableFloatProperty { get; set; }
    public double? NullableDoubleProperty { get; set; }
    public decimal? NullableDecimalProperty { get; set; }
    public DateTime? NullableDateTimeProperty { get; set; }
}

This will be applied to both FillWithEmptyValuesBehavior and FillWithSequentialValuesBehavior.

There is also a new option that allows disabling filling Nullable fields. This option can be useful to preserve the previous behavior.

new FillWithEmptyValuesBehavior(options => options.FillNullables = false)

Fixes virtual call from constructor

The method Customize(ICustomizeFactoryOptions<T> customization), implemented by customized factories that extend MagicFactory, was previously called from the constructor of MagicFactory.

This means the following setup would not work, because the Customize() method would be invoked before the constructor of the PersonFactory class finished execution:

new PersonFactory : MagicFactory<Person>
{
  private readonly int _age;

  public CustomFactory(int age) {
    _age = age;
  }

  protected override void Customize(ICustomizeFactoryOptions<Person> customization)
  {
      customization.Set(x => x.Age = _age);
  }
}