Giter Club home page Giter Club logo

factory-girl's Introduction

factory-girl

Build status

A library for setting up .NET objects as test data. Inspired by the original factory_girl for ruby, this implementation offers a simpler, feature-concise version for .NET that was influenced by FactoryGirl.NET and Plant.

Getting Started

Install factory-girl from Nuget

Install-Package factory-girl

Documentation

In the beginning, there was a model

Model

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

To get started, implement the IDefinable interface.

Factory

public class UserFactory : IDefinable
{
    public void Define()
    {
        FactoryGirl.Define<User>(() => new User
        {
            Name = "Bruce Wayne",
            Address = "Wayne Manor, Gotham City"
        });
    }
}

By implementing the interface, you can initialize all your factories in the given Type's assembly by calling the Initialize method.

FactoryGirl.Initialize(typeof(UserFactory));

Default and Named Factories

For each Type you can define two types of factories: Default or Named. You can have as many uniquely Named factories for a given type as you would like but you can only have one Default type.

Default Factory

public void DefineDefaultUser()
{
    FactoryGirl.Define<User>(() => new User
    {
        Name = "Peter Griffin",
        Address = "31 Spooner Street, Quahog, RI"
    });
}

Named Factories

private void DefineSeriousUser()
{
    FactoryGirl.Define("SeriousUser", () => new User
    {
        Name = "Sirius Black",
        Address = "12 Grimmauld Place"
    });
}
private void DefineAdminUser()
{
    FactoryGirl.Define("OldUser", () => new User
    {
        Name = "Fred Flinstone",
        Address = "301 Cobblestone Way"
    });
}

Building Test Objects

Building test objects of your models is now possible.

To build a Default User:

FactoryGirl.Build<User>();

To build a Named User:

FactoryGirl.Build<User>("SeriousUser");

You can also build a list of objects by calling the BuildList method

FactoryGirl.BuildList<User>();

If you need to clear your Factory call the ClearFactoryDefinitions method

FactoryGirl.ClearFactoryDefinitions();

Persisting Objects

To persist your object, implement the IRepository interface and use the Create and CreateList methods. It is important to remember that, like the original factory_girl, this implementation only calls Save() on the model. What Save() does is up to you.

public class User : IRepository
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    
    public User Save()
    {
        // Your logic for saving the model to a Database.
        // Or calling a web service.
        // Or whatever persistence means to you!
        return this;
    }
}

Now we can call Create and CreateList on our Factory

var user = FactoryGirl.Create<User>();
// Create 5 Users
var users = FactoryGirl.CreateList<User>(5);

factory-girl's People

Contributors

uchagani avatar bobafett0 avatar starrett67 avatar

Stargazers

seawish avatar Dmitry Razumikhin avatar Mikhail Brinchuk avatar Paul DeVito avatar Kai avatar Daniel Groves avatar Ben D'Angelo avatar Neo avatar Abdourakhmane Ripault avatar Rafał Jasica avatar

Watchers

James Cloos avatar  avatar  avatar  avatar

factory-girl's Issues

FactoryGirl singleton

Could you please provide a singleton version of FactoryGirl whose only static member is a data member called self? Since C# will never be fully-OO, this compromise would help when using XUnit Test Fixtures.

Need documentation for associations

The README does not show how to do associations with this package. For example if I have models Foo and Bar

public class Foo
{
    public Guid BarID { get; set; }
    public Bar Bar { get; set; }
}

public class Bar
{
    public string Name { get; set; }
}

And I have a factory for Bar

public class BarFactory : IDefinable
{
    public void Define()
    {
        FactoryGirl.Define<Bar>(() => new Bar
        {
            Name = "Baz"
        });
    }
}

Then how do I assign Bar in FooFactory?

public class FooFactory : IDefinable
{
    public void Define()
    {
        FactoryGirl.Define<Foo>(() => new Foo
        {
            Bar = // ????
        });
    }
}

Allow user to define persistence strategy

Currently we allow persistence according to the ActiveRecord pattern by calling Save() on the model itself. While this is fine for now, it would be more advantageous if we allowed the user to define the persistence strategy.

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.