Assist Unit Testing with predicatble auto-generated data


beyond basics

Once you get to grips with the basics, try out some more advanced features.

Custom Extensions

Custom commands can be added using extension methods as follows. These command extensions apply to both in-line and global configuration.

public static TReturn TestPerson<TReturn>(this TReturn autoExpression)
        where TReturn : IAutoConfiguration
    autoExpression.Set<Person>(o => o.FirstName, "TestUser")
                  .Set<Person>(o => o.LastName, "TestUserLastname")
                  .Set<Person>(o => o.Email, "");

    return autoExpression;

The extension can then be used to create custom instances as follows.

Person person = Auto.Make<Person>().TestPerson();

Assert.That(person, Is.Not.Null);
Assert.That(person.FirstName, Is.EqualTo("TestUser"));
Assert.That(person.LastName, Is.EqualTo("TestUserLastname"));
Assert.That(person.Email, Is.EqualTo(""));

Extend Global Commands

Custom global commands can be added using extension methods as follows.

public static DefaultAutoConfiguration SetTestPerson(this DefaultAutoConfiguration autoExpression)
    int id = 0;
    autoExpression.With<Person>(() => 
    new Person {Id = id++, FirstName="FirstName"+id, LastName="LastName"+id});

    return autoExpression;

The extension can then be used to create custom instances as follows.


Person person = Auto.Make<Person>();

Assert.That(person, Is.Not.Null);
Assert.That(person.Id, Is.EqualTo(0));
Assert.That(person.FirstName, Is.EqualTo("FirstName-0"));
Assert.That(person.LastName, Is.EqualTo("LastName-0"));

Person person2 = Auto.Make<Person>();

Assert.That(person2, Is.Not.Null);
Assert.That(person2.Id, Is.EqualTo(1));
Assert.That(person2.FirstName, Is.EqualTo("FirstName-1"));
Assert.That(person2.LastName, Is.EqualTo("LastName-1"));

Fluent Configuration

Configuration of predictable test data can be set using the fluent interface either globally or in-line as shown in the example below.

Person person = Auto.Make<Person>().Min();

Assert.That(person.Id, Is.EqualTo(int.MinValue));

The same configuration set globally.


Person person = Auto.Make<Person>();

Assert.That(person.Id, Is.EqualTo(int.MinValue));

Configuration commands can also be chained together using the fluent interface.

 Person person = Auto.Make<Person>()
                     .Do(o => o.Father = new Person { FirstName = "FatherName"})

 Assert.That(person.Id, Is.EqualTo(15));
 Assert.That(person.Father.FirstName, Is.EqualTo("FatherName"));
 Assert.That(person.Children.Count, Is.EqualTo(2));
 Assert.That(person.Children[0].FirstName, Is.EqualTo("FirstName"));

Proxy Classes

The library will automatically generate proxy classes to enable the creation of interface and abstract class types.

When creating instances from an interface the instance must be cast explicitly as interfaces cannot be implicitly cast. Do not use the var keyword without referencing the created instance explicitly with Object.

IPerson person = Auto.Make<IPerson>().Object;
var person = Auto.Make<IPerson>().Object;

All public members in the proxy class will be recursively populated with test data. By default strings are set to the name of the containing member, and value types are set to their corresponding MaxValue.

In addition any methods created on proxy classes with also be given auto generated return values.