-
Notifications
You must be signed in to change notification settings - Fork 1
Example usage
Overview | Example Usage
Auto generate objects, with predictable pre-populated fields as follows utilising the [default behaviour|Default Behaviour].
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 the expression is implicitly cast to the instance. If using the var keyword, then always reference the instance explicitly using Object.
var person = Auto.Make<Person>().Object;
Interfaces can't be implicitly cast, therefore always use Object.
IPerson person = Auto.Make<IPerson>().Object;
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"));
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"));
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"));
Override to use default values for all value types.
Person person = Auto.Make<Person>()
.Default();
Assert.That(person.Id, Is.EqualTo(0));
Override to use Max values for all value types.
Person person = Auto.Make<Person>()
.Max();
Assert.That(person.Id, Is.EqualTo(int.MaxValue));
Override to use Min values for all value types.
Person person = Auto.Make<Person>()
.Min();
Assert.That(person.Id, Is.EqualTo(int.MinValue));
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 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/")
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();
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"))