AutoObjectBuilder


Assist Unit Testing with predicatble auto-generated data

Usage

quick start

The following guide shows how to get up and running with AutoObjectBuilder.Net as quicky as possible.

Installation

Install the AutoObjectBuilder.Net library using Nuget.

PM> Install-Package AutoObjectBuilder

Auto-Generate Objects

Create an instance of any class using the library’s Generic interface. The return object is implicitly cast to the generic type, or can be referenced explicitly using the Object property.

Do not use the var keyword without casting the instance, or referencing the created instance explicitly.

// Implicit Casting
Person person = Auto.Make<Person>();

// Explicit Casting
var person = Auto.Make<Person>().Object;
var person = (Person)Auto.Make<Person>();

Auto-generated objects are populated with predictable data which can be leveraged in assert statements.

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

Assert.That(person.FirstName, Is.EqualTo("FirstName"));
Assert.That(person.Child.FirstName, Is.EqualTo("FirstName"));
Assert.That(p.PersonUrl.ToString(), Is.EqualTo("http://personurl/");
Assert.That(p.Person.Children.Count, Is.EqualTo(10));

By default members of type string return the name of the member.

Commands

The following table shows the configuration commands which can be used to override default behaviour and set up custom data.

Configuration Commands Description
With<T>(T value) Sets the default value for the type T.
With<T>(Func<T> factory) Sets a factory delegate for the creation for type T.
With<T>(Func<Type, T> factory) Sets a factory delegate for the creation for type T based on Type T.
Setter<T>(Func<MemberInfo, T> setter) Sets a member of type T to a value based on the associated MemberInfo instance.
Setter(Func<MemberInfo, T> setter) Sets a member of type the Make type T to a value based on the associated MemberInfo instance.
Do<T>(Action<T> expression) A delegate to invoke against a new instance of type T. Use instead of Set to override members.
Do(Action<T> expression) A delegate to invoke against the Make type T. Use instead of Set to override members.
Set<T>(Expression<Func<T, object>> expression, object value) Sets a member to the given value for type T. Obsolete use Do.

Extended Commands

The following table shows the custom configuration commands that can be used when creating an instance. These commands can be extended.

Extended Commands Description
Max() Sets all supported values types to their MaxValue. Enum types are set to the last item, bool is set to true. This is the default.
Min() Sets all supported values types to their MinValue. Enum types are set to the first item, bool is set to false.
Default() Sets all supported values types to their Default. Enum types are set to the first item.
EnumerationSize(count) Populates any ICollection, IList, or Array types with the given number of items. Default is 10. If size is greater than 0 then IDictionary is populated with a single item.