Skip to content
Anton Setiawan edited this page Sep 3, 2014 · 2 revisions

Overview

A spec in NDummy determines how an instance of specific class should be generated.

To create a spec, you need to create new class which derives from FactorySpec<T> and register it to FactoriesConfig instance.

After a spec is being registered, that spec will always be a reference for generating type it represents, unless if more detailed spec is defined (e.g.: property spec) .

public class B
{
	public string Id {get; set;}
	...
}

public class A
{
	public B Property1 {get; set;}
	...
}

public class SpecA : FactorySpec<A>
{
	public SpecA()
	{ 
		...
	}
}

public class SpecB : FactorySpec<B>
{
	public SpecB()
	{ 
		...
	}
}

In the above example, you have 2 specs, SpecA for class A, and SpecB for class B.
If you don't specify how to generate Property1 in SpecA, then its value will be generated by SpecB.

A spec can define the following things:

  • constructor
  • temporary values (a.k.a temporary properties)
  • public members (fields/properties) values
  • fields / properties to ignore
  • action to do after object is created

Constructor Spec

Constructor spec defines how an instance of specified type is created.
If your type have arguments in its constructor then you need to supply constructor spec.
To create a constructor spec, you need to call ConstructWith method.

public class SpecA : FactorySpec<A>
{
	public SpecA()
	{ 
		ConstructWith(()=> new A(1.2)); // constructor spec
	}
}

Temporary Properties

Temporary properties are additional values which are evaluated during the creation of object.
With temporary properties, you can store values not available in object itself and you can use them to initialize properties and fields.

To configure temporary properties, you need to call ConfigureTemporaryProperties method, and TemporaryProperty.New to define each property.

public class SpecA : FactorySpec<A>
{
	public SpecA()
	{
		ConfigureTemporaryProperties(
			//new temp property named prop1 with value "Hello World"
			TemporaryProperty.New("prop1", "Hello World"),

			//new temp property named prop2 with value generated with IFactory<T>
			TemporaryProperty.New<Guid>("prop2", new GuidFactory()),

			//new temp property named prop3 with value generated with Func<ObjectCreationContext,T>			
			TemporaryProperty.New<string>("prop3", ctx => "#'+ctx.Index)

		);
	}
}

Temporary properties are accessible via ObjectCreationContext, which will be created for every new instance.
It has the following properties:

  • Index : current index (starts from 1). It shows how many object has been created.
  • CurrentObject: current instance
  • TempProperties: temporary properties information in IDictionary<string, object> .

Member Spec

Member spec defines how to generate value for public properties and fields.

To configure member spec, you need to call ConfigureProperties method.

public class SpecA : FactorySpec<A>
{
	public SpecA()
	{	
		ConfigureProperties(
			//new spec for "Speed" property with const value 100
			For(x =>x.Speed).GeneratedWith(100),

			//new spec for "Power" property, generated with IFactory<T>
			For(x =>x.Power).GeneratedWith(new ComplexPowerGenerator()) 	

			//new spec for "Id" property, generated with Func<ObjectCreationContext,T>
			For(x => x.Id).GeneratedWith(ctx => ctx.Index)		
		);
	}
}

Ignored Members Spec

If you don't specify a member spec, then NDummy will check whether that member's type has its own spec. If it has then NDummy will use it to initialize value for that member. Ignored members spec is used to ensure that specific field / property won't be initialized.

You need to call Ignore method to ignore a specific member.

public class SpecA : FactorySpec<A>
{
	public SpecA()
	{
		Ignore(x => x.Power);
	}
}

Post Action

Post action is executed whenever an object is created successfully.

You need to override PostAction method to set custom logic there.

public class SpecA : FactorySpec<A>
{
	public override void PostAction(ObjectCreationContext creationContext)
	{
		//place your custom logic here
	}
}

Clone this wiki locally