Skip to content
TheCodeKing edited this page Jul 16, 2011 · 3 revisions

Extend Commands

Custom commands can be added using extension methods as follows. These command extensions apply to both in-line and global configuration. The default commands are listed [here|Commands]:

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, "test@email.co.uk");

	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("test@email.co.uk"));

Extend Global Commands

Custom commands can be added using extension methods as follows. These command extensions apply to only the global configuration. The default commands are listed [here|Commands]:

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.

Auto.Configure.SetTestUser();

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"));

Clone this wiki locally