Skip to content

Commit 9266739

Browse files
committed
Added tests to ensure the correct behaviour for the factories with respect to using builder values
Changed the AllPropertiesFactory to call the constructor using builder values rather than using AutoFixture
1 parent 68810c9 commit 9266739

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

TestStack.Dossier.Tests/Factories/AllPropertiesFactoryTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,23 @@ public void GivenAllPropertiesFactory_WhenBuilding_ThenAllPropertiesSet()
1919
dto.NotSetByCtorWithPrivateSetter.ShouldNotBe(null);
2020
dto.NotSetByCtorWithPublicSetter.ShouldNotBe(null);
2121
}
22+
23+
[Fact]
24+
public void GivenAllPropertiesFactoryAgainstBuilderWithModifications_WhenBuilding_ThenCustomisationsAreUsed()
25+
{
26+
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>
27+
.CreateNew(new AllPropertiesFactory())
28+
.Set(x => x.SetByCtorNoPropertySetter, "0")
29+
.Set(x => x.SetByCtorWithPrivateSetter, "1")
30+
.Set(x => x.SetByCtorWithPublicSetter, "2")
31+
.Set(x => x.NotSetByCtorWithPrivateSetter, "3")
32+
.Set(x => x.NotSetByCtorWithPublicSetter, "4");
33+
34+
dto.SetByCtorNoPropertySetter.ShouldBe("0");
35+
dto.SetByCtorWithPrivateSetter.ShouldBe("1");
36+
dto.SetByCtorWithPublicSetter.ShouldBe("2");
37+
dto.NotSetByCtorWithPrivateSetter.ShouldBe("3");
38+
dto.NotSetByCtorWithPublicSetter.ShouldBe("4");
39+
}
2240
}
2341
}

TestStack.Dossier.Tests/Factories/AutoFixtureFactoryTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using Shouldly;
1+
using Shouldly;
32
using TestStack.Dossier.Factories;
43
using TestStack.Dossier.Tests.TestHelpers.Objects.Examples;
54
using Xunit;

TestStack.Dossier.Tests/Factories/CallConstructorFactoryTests.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using Shouldly;
1+
using Shouldly;
32
using TestStack.Dossier.Factories;
43
using TestStack.Dossier.Tests.TestHelpers.Objects.Examples;
54
using Xunit;
@@ -9,7 +8,7 @@ namespace TestStack.Dossier.Tests.Factories
98
public class CallConstructorFactoryTests
109
{
1110
[Fact]
12-
public void GivenConstructorFactory_WhenBuilding_ThenOnlyConstructorPropertiesSet()
11+
public void GivenCallConstructorFactory_WhenBuilding_ThenOnlyConstructorPropertiesSet()
1312
{
1413
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>.CreateNew(new CallConstructorFactory());
1514

@@ -19,5 +18,23 @@ public void GivenConstructorFactory_WhenBuilding_ThenOnlyConstructorPropertiesSe
1918
dto.NotSetByCtorWithPrivateSetter.ShouldBe(null);
2019
dto.NotSetByCtorWithPublicSetter.ShouldBe(null);
2120
}
21+
22+
[Fact]
23+
public void GivenCallConstructorFactoryAgainstBuilderWithModifications_WhenBuilding_ThenCustomisationsAreUsed()
24+
{
25+
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>
26+
.CreateNew(new CallConstructorFactory())
27+
.Set(x => x.SetByCtorNoPropertySetter, "0")
28+
.Set(x => x.SetByCtorWithPrivateSetter, "1")
29+
.Set(x => x.SetByCtorWithPublicSetter, "2")
30+
.Set(x => x.NotSetByCtorWithPrivateSetter, "3")
31+
.Set(x => x.NotSetByCtorWithPublicSetter, "4");
32+
33+
dto.SetByCtorNoPropertySetter.ShouldBe("0");
34+
dto.SetByCtorWithPrivateSetter.ShouldBe("1");
35+
dto.SetByCtorWithPublicSetter.ShouldBe("2");
36+
dto.NotSetByCtorWithPrivateSetter.ShouldBe(null);
37+
dto.NotSetByCtorWithPublicSetter.ShouldBe(null);
38+
}
2239
}
2340
}

TestStack.Dossier.Tests/Factories/PublicPropertySettersFactoryTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,33 @@ namespace TestStack.Dossier.Tests.Factories
88
public class PublicPropertySettersFactoryTests
99
{
1010
[Fact]
11-
public void GivenPublicPropertiesFactory_WhenBuilding_ThenOnlyConstructorAndPublicPropertiesSet()
11+
public void GivenPublicPropertySettersFactory_WhenBuilding_ThenOnlyConstructorAndPublicPropertiesSet()
1212
{
1313
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>.CreateNew(new PublicPropertySettersFactory());
1414

1515
dto.SetByCtorNoPropertySetter.ShouldNotBe(null);
1616
dto.SetByCtorWithPrivateSetter.ShouldNotBe(null);
1717
dto.SetByCtorWithPublicSetter.ShouldNotBe(null);
18+
dto.NotSetByCtorWithPrivateSetter.ShouldBe(null);
1819
dto.NotSetByCtorWithPublicSetter.ShouldNotBe(null);
20+
}
21+
22+
[Fact]
23+
public void GivenPublicPropertySettersFactoryAgainstBuilderWithModifications_WhenBuilding_ThenCustomisationsAreUsed()
24+
{
25+
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>
26+
.CreateNew(new PublicPropertySettersFactory())
27+
.Set(x => x.SetByCtorNoPropertySetter, "0")
28+
.Set(x => x.SetByCtorWithPrivateSetter, "1")
29+
.Set(x => x.SetByCtorWithPublicSetter, "2")
30+
.Set(x => x.NotSetByCtorWithPrivateSetter, "3")
31+
.Set(x => x.NotSetByCtorWithPublicSetter, "4");
32+
33+
dto.SetByCtorNoPropertySetter.ShouldBe("0");
34+
dto.SetByCtorWithPrivateSetter.ShouldBe("1");
35+
dto.SetByCtorWithPublicSetter.ShouldBe("2");
1936
dto.NotSetByCtorWithPrivateSetter.ShouldBe(null);
37+
dto.NotSetByCtorWithPublicSetter.ShouldBe("4");
2038
}
2139
}
2240
}

TestStack.Dossier/Factories/AllPropertiesFactory.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
namespace TestStack.Dossier.Factories
44
{
55
/// <summary>
6-
/// Creates an instance of an object by setting all public and private properties.
6+
/// Builds the object using the constructor with the most arguments using values stored in the builder that match
7+
/// the constructor parameter name case insensitively and then sets all public and private property setters with
8+
/// values from the builder.
9+
/// If there is no value specified in the builder for a ctor argument / property then the builder will supply an anonymous value.
710
/// </summary>
8-
public class AllPropertiesFactory : IFactory
11+
public class AllPropertiesFactory : CallConstructorFactory
912
{
1013
/// <inheritdoc />
11-
public TObject BuildObject<TObject, TBuilder>(TestDataBuilder<TObject, TBuilder> builder)
12-
where TObject : class
13-
where TBuilder : TestDataBuilder<TObject, TBuilder>, new()
14+
public override TObject BuildObject<TObject, TBuilder>(TestDataBuilder<TObject, TBuilder> builder)
1415
{
15-
var model = builder.Any.Fixture.Create<TObject>();
16+
var model = base.BuildObject(builder);
1617

1718
var properties = Reflector.GetSettablePropertiesFor<TObject>();
1819
foreach (var property in properties)

TestStack.Dossier/Factories/PublicPropertySettersFactory.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace TestStack.Dossier.Factories
44
{
55
/// <summary>
6-
/// Creates an instance of an object by setting all public properties but not private properties.
6+
/// Builds the object using the constructor with the most arguments using values stored in the builder that match
7+
/// the constructor parameter name case insensitively and then sets all public property setters with values from
8+
/// the builder.
9+
/// If there is no value specified in the builder for a ctor argument / property then the builder will supply an anonymous value.
710
/// </summary>
811
public class PublicPropertySettersFactory : CallConstructorFactory
912
{

0 commit comments

Comments
 (0)