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

Overview

NDummy is a simple, low-ceremony .NET library for generating objects based on specs.

NDummy is designed with simplicity in mind. It uses friendly configuration which will help user to define specs easier.

A common scenario for using NDummy is when you develop unit test.
In a typical Arrange-Act-Assert approach, you will need to supply dummy data during Arrange phase. If your model is complex, then you will find that writing it manually is a boring process.

Supported Platforms

NDummy is a portable class library so it supports the following platforms:

  • NET Framework 4.0 +
  • Silverlight 5
  • Windows Phone 8
  • Windows Store Apps

Installation

The easiest way to install NDummy is by using NuGet package in your project/solution.
Type this command in Package Manager Console

PM> Install-Package NDummy

Usage

####1. Create Spec or Custom Factory

NDummy requires spec for specific type before you can generate inctance of that type.
To define a spec, you need to create a class which derives from FactorySpec<T>.

public class WarriorSpec : FactorySpec<Warrior>
{
    public WarriorSpec()
    {
        ConfigureProperties(
           For(n =>n.Name).GenerateWith(ctx =>"Name-"+ctx.Index),
		   For(n =>n.Hp).GenerateWith(100) 	
          );
    }
}

Normally you don't need to create a custom factory, but sometimes you want to always assign certain value to specific type, or you have specific method to generate value for that type.
In this case, you just need create a new instance of FactoryMethod and supply it with a Func<T> as its constructor.

var stringFactory = new FactoryMethod<string>(()=>"Hello world")

If you want full flexibility you can also implement IFactory<T> interface.

####2. Register Specs and Custom Factories to FactoriesConfig

FactoriesConfig class holds information for types and its associated spec or factory.
You need to register specs and factories via FactoriesConfig's fluent interface.

var config = new FactoriesConfig()
	.ConfigureFactory(stringFactory)
	.Configure<Warrior, WarriorSpec>();

####3. Get the Generated Factory

The entry point for your app is FactoryManager class.
FactoryManager is responsible for generating a factory for you requested type.

var manager = new FactoryManager(config);
var factory = manager.GetFactory<Warrior>();

####4. Create The Instance Use factory to create instance as many times as you want

var instance = factory.Create();

Clone this wiki locally