Skip to content

Construktion is a library created to assist with unit testing by simplifying the arrange portion of your tests. It helps by creating objects and its properties with "junk" data. Inspired by AutoFixture

License

Notifications You must be signed in to change notification settings

micheleissa/Construktion

 
 

Repository files navigation

Build status NuGet Join the chat at https://gitter.im/Construktion_/Lobby

Quick Start

The Construktion class will build objects with randomized values. If you're using xUnit, you can inject constructed values automatically.

var construktion = new Construktion();

var string = construktion.Construct<string>();
var person = construktion.Construct<Person>();

string.ShouldNotBeNullOrWhiteSpace();
person.Name.ShouldStartWith("Name-");
person.Age.ShouldNotBe(0);

//overloads like ConstructMany<T> or ConstructMany<T>(Action<T> hardCodes) are available.

//xUnit isn't supported out of the box, but the wiki details
//how to add support
[Theory, ConstruktionData]
public void should_join_team(Team team, Player player)
{
    DbSave(player, team);
    
    player.JoinTeam(team);

    var foundPlayer = Query(db => db.Players.FirstOrDefault(x => x.Id == player.Id));
    foundPlayer.ShouldNotBeNull();
    foundPlayer.Name.ShouldBe(player.Name);
    foundPlayer.TeamId.ShouldBe(team.Id);
}

Customizing

At the heart of the library are the blueprints. They are used to customize how objects are built. Below is an example of a blueprint that will assign all bool properties named IsActive to true.

public class IsActiveBlueprint : Blueprint
{
    public bool Matches(ConstruktionContext context) => context.RequestType == typeof(bool) &&
                                                        context.PropertyInfo?.Name == "IsActive";

    public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) => true;
}

var construktion = new Construktion().With(new IsActiveBlueprint());

Customizations can be added through the construktion instance. For most scenarios the registry class should be used to group all configured settings.

var construktion = new Construktion().With(new CustomRegistry());

public class CustomRegistry : ConstruktionRegistry
{
    public CustomRegistry()
    {
        AddBlueprint<CustomBlueprint>();
        OmitIds();
        OmitProperties(typeof(List<>));
        Register<IService, Service>();
        //other blueprints and customization options
    }   
}

The wiki contains full details and documentation.

Questions and Comments

For any questions or help you can hop on over to the gitter room or file an issue. If you're currently using Construktion, I'd love to hear any feedback. Right now it's pretty much just me thinking of what people might want—instead of what they actually want.

About

Construktion is a library created to assist with unit testing by simplifying the arrange portion of your tests. It helps by creating objects and its properties with "junk" data. Inspired by AutoFixture

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.2%
  • HTML 0.8%