SparkyTestHelpers: Populater
Tags: csharp, dotnet, testing, unittesting
Published: 2020-02-23 13:13:39

> [NuGet package](https://www.nuget.org/packages/SparkyTestHelpers.Populater/) | [Source code](https://github.com/BrianSchroer/sparky-test-helpers/tree/master/SparkyTestHelpers.Populater) | [API documentation](https://github.com/BrianSchroer/sparky-test-helpers/blob/master/SparkyTestHelpers.Populater/api.md)

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

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

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

    var foo2 = populater.CreateRandom<Foo>();
```

##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

```csharp
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>();
```

##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:

```csharp
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>();
```