diff --git a/Source/SweetBuilders/BuilderBase.cs b/Source/SweetBuilders/BuilderBase.cs index e92ed6d..d167d70 100644 --- a/Source/SweetBuilders/BuilderBase.cs +++ b/Source/SweetBuilders/BuilderBase.cs @@ -19,28 +19,35 @@ public abstract class BuilderBase /// /// Initializes a new instance of the class. /// - protected BuilderBase() => this.Composer = this.Fixture.Build() - .FromFactory(Factories.Default); + protected BuilderBase() + : this(CreateComposer()) + { + } /// /// Initializes a new instance of the class. /// - /// A factory of the class. + /// A factory of . protected BuilderBase(Func factory) + : this(CreateComposer().FromFactory(factory)) { - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - var customizationComposer = this.Fixture.Build(); - this.Composer = customizationComposer.FromFactory(factory); } - private Fixture Fixture { get; } = new Fixture + /// + /// Initializes a new instance of the class. + /// + /// The fixture that will be used to initialize the builder. + protected BuilderBase(Fixture fixture) + : this(CreateComposer(fixture)) { - OmitAutoProperties = true, - }; + } + + /// + /// Initializes a new instance of the class. + /// + /// The composer that will be used by the builder. + protected BuilderBase(IPostprocessComposer? composer = null) + => this.Composer = composer ?? CreateComposer(); private IPostprocessComposer Composer { get; set; } @@ -285,4 +292,15 @@ public TBuilder Without(Expression> property /// The number of objects to create. /// A sequence of anonymous objects of type . public IEnumerable CreateMany(int count) => this.Composer.CreateMany(count); + + private static Fixture CreateFixture() => new() + { + OmitAutoProperties = true, + }; + + private static ICustomizationComposer CreateComposer(Fixture? fixture = null) + { + fixture ??= CreateFixture(); + return fixture.Build(); + } } diff --git a/Tests/SweetBuilders.Test/BuilderBaseTests.cs b/Tests/SweetBuilders.Test/BuilderBaseTests.cs index 39f0957..d118a6f 100644 --- a/Tests/SweetBuilders.Test/BuilderBaseTests.cs +++ b/Tests/SweetBuilders.Test/BuilderBaseTests.cs @@ -268,30 +268,33 @@ public void ShouldCreateNInstancies() } [Fact] - public void ShouldThrowIfFactoryIsNull() + public void ShouldThrowIfPropertyNameIsNull() { - var act = () => new FooBarInvalidBuilder(); + var builder = Builder.New; + +#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. + var act = () => builder.WithPrivate(null, string.Empty); +#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. using (new AssertionScope()) { act.Should().Throw() - .WithParameterName("factory"); + .WithParameterName("propertyName"); } } [Fact] - public void ShouldThrowIfPropertyNameIsNull() + public void ShouldRespectFixtureCustomizations() { - var builder = Builder.New; - -#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. - var act = () => builder.WithPrivate(null, string.Empty); -#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. + var foo = new FooBuilderCustomFixture() + .Create(); using (new AssertionScope()) { - act.Should().Throw() - .WithParameterName("propertyName"); + foo.Bar.Should().NotBeNull("the builder should generate all public properties"); + foo.Bar?.Name.Should().Be( + FooBuilderCustomFixture.NAME, + "the builder should respect any Fixture customizations"); } } diff --git a/Tests/SweetBuilders.Test/FooBar.cs b/Tests/SweetBuilders.Test/FooBar.cs index 1b1b57b..118188a 100644 --- a/Tests/SweetBuilders.Test/FooBar.cs +++ b/Tests/SweetBuilders.Test/FooBar.cs @@ -1,6 +1,7 @@ namespace SweetBuilders.Test; using System; +using AutoFixture; #pragma warning disable CS0169, IDE0051 // Remove unused private members #pragma warning disable SA1649 // File name should match first type name @@ -53,14 +54,24 @@ internal class BarBuilder : BuilderBase public BarBuilder() => this.With(x => x.Name, NAME); } -internal class FooBarInvalidBuilder : BuilderBase +internal class FooBuilderCustomFixture : BuilderBase { -#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. - public FooBarInvalidBuilder() - : base(null) + internal static readonly string NAME = Guid.NewGuid().ToString(); + + public FooBuilderCustomFixture() + : base(CreateFixture()) + { + } + + private static Fixture CreateFixture() { + var fixture = new Fixture + { + OmitAutoProperties = false, + }; + fixture.Customize(bar => bar.With(x => x.Name, NAME)); + return fixture; } -#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. } #pragma warning restore CS0169, IDE0051 // Remove unused private members #pragma warning restore SA1649 // File name should match first type name