The TestDataBuilder pattern is used in unit testing to create collaborators. Its advantages versus using constructors:Changes to constructors are localized.Test code becomes clearer, because you don't have to specify all the required parameters to the constructor.A disadvantage is that the usage is somewhat lengthy which decreases readability:var service = serviceBuilder.WithRepo(    repoBuilder.WithEntities(      new [] { entityBuilder.WithName(name) }).Build())  .WithMailSender(mailSenderBuilder.Build())  .Build();The word With is typically repeated many times.Same for the call to Build().Same for the word Builder.Although all of them can be mitigated, I guess the solutions themselves have drawbacks. For instance, we could rename serviceBuilder to service, but this could give the wrong impression about the type of the variable. We could implement an implicit conversion from a builder class to the object it builds, but it is more code in the builder in addition to already abundant With* methods.Is there an alternative to TestDataBuilder that doesn't have these drawbacks?
What is a good alternative to the TestDataBuilder pattern?
c#;unit testing
My suggestion is to use the Factory pattern with a twist, taking advantage of default parameters. The implementation is more concise than that of a builder, the test code too.I call it MotherFactory. Here is the code:// ==== In a common test library ===/// <summary>/// Provides functionality to create test collaborators via extension methods./// </summary>/// <remarks>/// This class is a helper class to classes that implement the Factory pattern./// The purpose of this class together with the factories is to/// <list type=bullet>///    <item>///       <description>///       localize changes to constructors,///       </description>///    </item>///    <item>///       <description>///       make the test code clearer by not having to specify all///       the required parameters to the constructor,///       </description>///    </item>///    <item>///       <description>///       provide a concise syntax for object creation:///       <code>Entity e = an.Entity();</code>///       where <c>an</c> has type <see cref=MotherFactory/> and <c>Entity()</c> is an///       extension factory method with optional parameters.///       </description>///    </item>/// </list>/// </remarks>// ReSharper disable once ConvertToStaticClass : exists for extension methods.public class MotherFactory{    /// <summary>    /// Prevents a default instance of the <see cref=MotherFactory/> class from being created.    /// </summary>    private MotherFactory()    {    }}// ==== In a test project ===public static class MotherFactoryEntityExtensions{    public static Entity Entity(        this MotherFactory a,        int? year = 2014,        string name = name,        Dependency dependency = null,        withoutDependency = false)    {        if (withoutDependency && dependency != null)        {            throw new ArgumentException(                The parameter 'dependency' cannot be used when true is specified for 'withoutDependency'.,                dependency);        }        if(dependency == null && !withoutDependency)        {            dependency = a.Dependency();        }        return new Entity(startYear, endYear, name, summary, dependency);    }}public class EntityTest{    private static readonly MotherFactory an = null;    [Fact]    public void Ctor_GivenNullDependency_Throws()    {        Exception e = Record.Exception(() =>        {            Entity systemUnderTest = an.Entity(withoutDependency: true);        });        Assert.NotNull(e);    }}public class ServiceTest{    private static readonly MotherFactory a = null;    private static readonly MotherFactory an = null;    [Fact]    public void Find_GivenCorrectRequest_FindsEntity()    {        var service = a.Service(repo: a.Repository(dbEntities: new [] { an.Entity(name: XXX) });        var result = service.Find();        Assert.Equal(XXX, result.Name);    }}NotesPut the MotherFactory class in a library and in test projects create a class with extension methods for each collaborator class.Parameters that have a prefix without (like withoutDependency) are used only in cases when we want to disable creating the default instance of a dependency and pass null instead.A MotherFactory*Extensions class can also be an ObjectMother by providing methods like UserWithCompleteProfile().