AutoObjectBuilder


Assist Unit Testing with predicatble auto-generated data

Reference

commands

The following section details the AutoObjectBuilder members and behaviour.

With Command

Override a default value for a given type.

Person person = Auto.Make<Person>()
                    .With<string>("Default text for strings");

Assert.That(person.FirstName, Is.EqualTo("Default text for strings"));

Override a default value using a factory delegate.

int id = 100;
Person person = Auto.Make<Person>()
                    .With<string>(()=> "FirstName-"+id);

Assert.That(person.FirstName, Is.EqualTo("FirstName-100"));

Do Command

Override a member with a explicit value. Do takes precedence over other conflicting With, Set and Setter commands.

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

Assert.That(person.Father.FirstName, Is.EqualTo("Father"));

Setter Command

Override a member value based on it’s MemberInfo instance. Setter takes precedence over conflicting With commands.

Person person = Auto.Make<Person>()
                    .Setter<string>(m => "MemberName: "+m.Name);

Assert.That(person.FirstName, Is.EqualTo("MemberName: FirstName"));

Default Command Extension

Override to use default values for all value types.

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

Assert.That(person.Id, Is.EqualTo(0));

Max Command Extension

Override to use Max values for all value types.

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

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

Min Command Extension

Override to use Min values for all value types.

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

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

EnumerableSize Command Extension

Override the number of auto-populated enumerable types.

// create an Array type, populated with a defined number of items
Person[] person = Auto.Make<Person[]>()
                    .EnumerableSize(3);

Assert.That(person, Is.Not.Null);
Assert.That(person.Length, Is.EqualTo(3));

Chaining Commands

Chaining overrides together.

Person person = Auto.Make<Person>()
                    .Min()
                    .Do(o => o.Email = "xxxxxx.xxxxxx@xxxx.xxx") 
                    .With<Uri>(new Uri("http://www.thecodeking.co.uk"));

Assert.That(person.LastName, Is.Empty);
Assert.That(person.Email, Is.EqualTo("xxxxxx.xxxxxx@xxxx.xxx"));
Assert.That(person.Id, Is.EqualTo(int.MinValue));
Assert.That(person.PersonUrl.ToString(), Is.EqualTo("http://www.thecodeking.co.uk/")

Global Configuration

Override the default behaviour globally.

Auto.Configure
       .Min()
       .Do(o => o.Email = "xxxxxx.xxxxxx@xxxx.xxx") 
       .With<Uri>(new Uri("http://www.thecodeking.co.uk"));

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

Assert.That(person.LastName, Is.Empty);
Assert.That(person.Email, Is.EqualTo("xxxxxx.xxxxxx@xxxx.xxx"));
Assert.That(person.Id, Is.EqualTo(int.MinValue));
Assert.That(person.PersonUrl.ToString(), Is.EqualTo("http://www.thecodeking.co.uk/")

Reset the Global behaviour to default. This erases all custom configuration settings.

Auto.Configure.UseDefaultConfiguration();

Complex Example

The examples sets up a factory for the type Person, and increments the Id and values on every instance creation.

int id = 0;
Auto.Configure.With<Person>(() => 
        new Person {Id = id++, FirstName="FirstName"+id, LastName="LastName"+id});

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

Assert.That(value1.Id, Is.EqualTo(0));
Assert.That(value1.FirstName, Is.EqualTo("FirstName-0"));
Assert.That(value1.LastName, Is.EqualTo("LastName-0"))

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

Assert.That(value1.Id, Is.EqualTo(1));
Assert.That(value1.FirstName, Is.EqualTo("FirstName-1"));
Assert.That(value1.LastName, Is.EqualTo("LastName-1"))