Giter VIP home page Giter VIP logo

plant's Introduction

Plant

Plant is a test factory for .NET 4.0. It is very much like FactoryGirl (http://github.com/thoughtbot/factory_girl/) for Ruby. The goal of this project is to allow you to reduce noise and duplication across your tests when creating models.

Download

Features

Currently Plant supports

  • Object creation via properties
  • Object creation by constructor arguments
  • Overriding default property and constructor argument values
  • Modular object definition
  • Lazily evaluated property and constructor argument values
  • Sequenced properties
  • Allow user to specify after-create actions on models (to save the model to the DB after creation, for instance)

Targetted features can be found in the issues list for the project. Some specific ones are

  • Allowing multiple different definitions for one object
  • Specify associated instances

Terms

A 'Plant' is the thing that creates your objects for you.
A 'Blueprint' is what a user provides to tell a plant how to create an object.

Defining a Blueprint

A Blueprint is just a class that implements the Blueprint interface. The interface defines one method, SetupPlant, which takes a BasePlant object. SetupPlant is a generic method which whose generic argument is the Type that you're setting up and an anonymous object with the appropriate properties.

Note that currently property validation occurs during object creation, not DefinePropertiesOf.

class PersonBlueprint : Blueprint
{
  public void SetupPlant(BasePlant plant)
  {
    plant.DefinePropertiesOf<Person>(new
                           {
                              FirstName = "Barbara",
                              MiddleName = "Elaine",
                              LastName = "Brechtel",
                              Address = "111 South Main St.",
                              City = "Gulfport",
                              State = "MS",
                              EmailAddress = "[email protected]"
                           });
  }
}

Use DefineConstructionOf instead of DefinePropertiesOf when an object should be created via constructor arguments.

class PersonBlueprint : Blueprint
{
  public void SetupPlant(BasePlant plant)
  {
    plant.DefineConstructionOf<Person>(new
                           {
                              FirstName = "Barbara",
                              MiddleName = "Elaine",
                              LastName = "Brechtel",
                              Address = "111 South Main St.",
                              City = "Gulfport",
                              State = "MS",
                              EmailAddress = "[email protected]"
                           });
  }
}

Lazily evaluated properties

To define a Blueprint with a lazily evaluated property, set the value to new LazyProperty(lambda) like so:

class PersonBlueprint : Blueprint
{
  public void SetupPlant(BasePlant plant)
  {
    plant.DefinePropertiesOf<Person>(new
                           {
                              UniqueID = new LazyProperty<int>(() => IDGenerator.GenerateNewID())
                           });
  }
}

where TPropertyType (int in this case) is the type of the property and also that returned from the lambda.

Sequenced properties

To define a Blueprint property that is evaluated lazily, but with a sequence counter, set the value to new Sequence(lambda) like so:

class PersonBlueprint : Blueprint
{
  public void SetupPlant(BasePlant plant)
  {
    plant.DefinePropertiesOf<Person>(new
                           {
                              ID = new Sequence<int>((sequenceValue) => sequenceValue)
                              Name = new Sequence<string>((sequenceValue) => "test: " + sequenceValue)
                           });
  }
}

where TPropertyType (int in this case) is the type of the property and also that returned from the lambda.

Usage

To create a new Plant, you'll typically want to tell it which Assembly to look in for Blueprints. You can do this via

var plant = new BasePlant().WithBlueprintsFromAssemblyOf<PersonBlueprint>();

where PersonBlueprint is one of the Blueprints you have defined. Plant will then load blueprints from any other type that implements the Blueprint interface in that assembly.

To retrieve the default instance of an object

var person = plant.Create<Person>();

To retrieve an instance of a person with specific parts of the default blueprint overridden

var person = plant.Create<Person>(new { EmailAddress = "[email protected]" });

Multiple properties can be overridden in one call

var person = plant.Create<Person>(new { EmailAddress = "[email protected]", State = "GA" });

plant's People

Contributors

jbrechtel avatar jeffdeville avatar lcrodriguez avatar jagregory avatar jplindgren avatar harriyott avatar

Watchers

Jeremiah Medina avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.