DEV Community

Cover image for SparkyTestHelpers: Populater
Brian Schroer
Brian Schroer

Posted on

SparkyTestHelpers: Populater

NuGet package | Source code | API documentation

This package provides tools for populating class instance properties for testing purposes, usually with random values:

using SparkyTestHelpers.Population;
. . .
    var populater = new Populater();

    var foo = new Foo();
    populater.PopulateWithRandomValues(foo);

    var foo2 = populater.CreateRandom<Foo>();
Enter fullscreen mode Exit fullscreen mode

GetRandom

Populater is usually called via the static GetRandom static method, which creates:

  • random instances of .NET intrinsic types:

    • GetRandom.Bool()
    • GetRandom.DateTime()
    • GetRandom.Decimal()
    • GetRandom.DateTime()
    • GetRandom.Int()
    • GetRandom.IntegerInRange(minValue, maxValue)
    • GetRandom.Long()
    • (etc.)
  • random instances of enum values:

    • GetRandom.EnumValue<TEnum>()
  • randomly populated instances of any class type:

    • GetRandom.InstanceOf<T>()
    • GetRandom.InstanceOf<T>(Action<T>) - This override passes the created instance to a "callback" action where additional initialization can be performed.
  • random collections of a specified type:

    • GetRandom.ArrayOf<T>(size)
    • GetRandom.ListOf<T>(size)
    • GetRandom.IEnumerableOf<T>(size)
    • These methods all have overrides with a "callback" action that can be used to perform additional initialization, e.g. GetRandom.ArrayOf<T>(size, Action<Array<T>>)

You can call GetRandom.ValuesFor<T>(T existingInstance) to randomly populate the values of an existing class instance.

Examples

using SparkyTestHelpers.Population;
. . .
    // Create new random instance:
    var foo = GetRandom.InstanceOf<Foo>();

    // Randomly populate properties of existing instance:
    var foo2 = new Foo();
    GetRandom.ValuesFor(foo2);

    // Create new random instance, but override specific property(s):
    var foo3 = GetRandom.InstanceOf<Foo>(x => x.StartTime = DateTime.Now);

    // Create array of 3 random instances:
    var foos = GetRandom.ArrayOf<Foo>(3);

    // Create array of random instances, with callback for additional initialization:
    var foos2 = GetRandom.ListOf<Foo>(10, list => 
        list.ForEach(item => item.StartTime = DateTime.Now):

    // Intrinsic types and enums:
    var number = GetRandom.Int();
    var diceRoll = GetRandom.IntegerInRange(1, 6);
    var dt = GetRandom.DateTime();
    var amount = GetRandom.Decimal();
    var enumValue = GetRandom.EnumValue<StringComparisonType>();
Enter fullscreen mode Exit fullscreen mode

SequentialValueProvider

The package also has a SequentialValueProvider that auto-populates class properties with predictable/repeatable values.

I use it for testing an HTML report builder where I want to regression test changes to the builder string output. I don't want to hand-code the input model properties, but I don't want to use random values, because that would generate a different HTML string each test:

using SparkyTestHelpers.Population;
. . .
    var populater = new Populater();

    // Sequentially populating existing instances:

    var foo = new Foo();
    populater.Populate(foo, new SequentialValueProvider()); 

    // (SequentialValueProvider is the default provider for the "Populate" method:
    var foo2 = new Foo();
    populater.Populate(foo);

    // Creating a new sequentially populated instance:
    var foo3 = populater.CreateAndPopulate<Foo>();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)