LochNessBuilder 2.1.0

Additional Details

API has been drastically updated to v4.0.0; Only that version is going to receive further support. Please upgrade to v4.0.0; the README has a detailed migration guide.

There is a newer version of this package available.
See the version list below for details.
dotnet add package LochNessBuilder --version 2.1.0
NuGet\Install-Package LochNessBuilder -Version 2.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LochNessBuilder" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LochNessBuilder --version 2.1.0
#r "nuget: LochNessBuilder, 2.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LochNessBuilder as a Cake Addin
#addin nuget:?package=LochNessBuilder&version=2.1.0

// Install LochNessBuilder as a Cake Tool
#tool nuget:?package=LochNessBuilder&version=2.1.0

LochNessBuilder

A C# Builder library to automate creation of objects for tests

Migration Guide

Do not use v2.0.0

This version was released inadvertantly and has been deprecated and de-listed from nuget.org

Versions 2.1 and 3.0-alpha are available

  • If you wanted "v1.x but on .NET Standard", then use v2.1.
  • If you really wanted "the version that was released as v2.0.0" then use v3.0-alpha, though this is not release-ready and should only be used if you've already integrated against the inadvertant v2.0.0 release.

Migration from v1.0 to v2.1

No API changes. No Migration required. MIT License was added.

Use in a .Net Framework project

As of version 2.0, this library has been migrated to .Net standard, to enable use in .Net core projects.

To continue consuming this library from a .Net Framework project, the following reference will need to be added to the .csproj file:

<Reference Include="netstandard" />

Examples

The builder for a number of Monsters:

using System.Linq;
using LochNessBuilder;

[Builder]
public static class MonsterBuilder
{
    public static Builder<Monster> New
    {
        get
        {
            return Builder<Monster>.New
                .With(t => t.Id, Enumerable.Range(1, int.MaxValue))
                .With(t => t.Name, Enumerable.Range(1, int.MaxValue).Select(i => $"Name {i}"));
        }
    }
}
  
public class Monster
{
  public int Id { get; set; }
  public string Name { get; set; }
}

How you would then build a number of Monsters for use in tests (using types in place of var for clarity):

// To build a single Monster
Monster testMonster = MonsterBuilder.New.Build();
// testMonster has Id = 1, Name = "Name 1"
// To build an enumerable of Monsters
IEnumerable<Monster> testMonsters = MonsterBuilder.New.Build(5);
// testMonsters has 5 Monsters, with Ids of respectively 1, 2, 3, 4, 5 and matching Names
// To build an enumerable of Monsters, each with a fixed Name
IEnumerable<Monster> nameMonsters = MonsterBuilder.New.WithPostBuildSetup(t => t.Name, "Constant").Build(4);
// nameMonsters have Ids 1-4 but all have name "Constant"
// To build an enumerable of Monsters, each with a name matching the previous monster's Id
string prevName = "0";
IEnumerable<Monster> seqMonsters = MonsterBuilder.New
    .WithSetup(t => t.Name = prevName)
    .WithPostBuildSetup(t => prevName = t.Id.ToString()
    .Build(3);
// seqMonsters have Ids 1, 2, 3 and Names "0", "1", "2"

Sometimes you may need a stateful Builder - for example, if there are constraints on uniqueness of certain properties.

// In the enclosing class
private Builder<Monster> monsterBuilder = MonsterBuilder.New;
// Then use this form in methods instead
Monster testMonster = monsterBuilder.Build();

Variations on the Builder

The builder can also be customised to build things differently. You can use factories (withFactory), setup (withSetup), post-build setup (withPostBuildSetup), collections (withCollection)

TODOs

  • Update with more detailed documentation for Builder
  • Better examples
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.0 40,805 12/31/2021
4.0.0 1,061 11/7/2021
2.1.0 738 8/29/2021
1.0.1 20,276 9/23/2015
1.0.0 1,198 9/21/2015