Skip to content

Commit 48acf15

Browse files
committed
Added documentation for sharing anonymous value fixtures across builders
1 parent 775f87b commit 48acf15

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,39 @@ The `BuildUsing` method takes an instance of `IFactory`, of which you can create
225225
* `CallConstructorFactory` - Calls the longest constructor with builder values (or anonymous values if none set) based on case-insensitive match of constructor parameter names against property names
226226
* `AutoFixtureFactory` - Asks AutoFixture to create an anonymous instance of the class (note: does **not** use any builder values or anonymous values from Dossier)
227227

228+
Propogating the anonymous value fixture across builders
229+
-------------------------------------------------------
230+
231+
Within a particular instance of `AnonymousValueFixture`, which is created for every builder, any generators that return a sequence of values (e.g. unique values) will be maintained. If you want to ensure that the same anonymous value fixture is used across multiple related builders then:
232+
233+
* Using `CreateListOfSize` will automatically propagate the anonymous value fixture across builders
234+
* Call the `GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null)` method from within your custom builder, e.g.:
235+
236+
public MyCustomBuilder WithSomeValue(Func<SomeBuilder, SomeBuilder> modifier = null)
237+
{
238+
return Set(x => x.SomeValue, GetChildBuilder<SomeObject, SomeBuilder>(modifier));
239+
}
240+
* If using `Builder<T>` then call the `SetUsingBuilder` method, e.g.:
241+
242+
// Uses Builder<T>
243+
Builder<StudentViewModel>.CreateNew()
244+
.SetUsingBuilder(x => x.Address)
245+
.Build()
246+
// Uses Builder<T>, includes customisation
247+
Builder<StudentViewModel>.CreateNew()
248+
.SetUsingBuilder(x => x.Address, b => b.Set(y => y.Street, "A street"))
249+
.Build()
250+
// Uses AddressBuilder
251+
Builder<StudentViewModel>.CreateNew()
252+
.SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address)
253+
.Build()
254+
// Uses AddressBuilder, includes customisation
255+
Builder<StudentViewModel>.CreateNew()
256+
.SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address, b => b.Set(y => y.Street, "A street"))
257+
.Build()
258+
259+
There is currently no way to share an anonymous value fixture across unrelated builder instances. If this is something you need please raise an issue so we can discuss your requirement.
260+
228261
Anonymous Values and Equivalence Classes
229262
----------------------------------------
230263

TestStack.Dossier.Tests/ChildBuilderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public class ParentBuilder : TestDataBuilder<ParentObject, ParentBuilder>
6868
{
6969
public ParentBuilder()
7070
{
71-
Set(x => x.Child, new ChildBuilder().Build());
71+
Set(x => x.Child, new ChildBuilder());
7272
}
7373

7474
public ParentBuilder WithChildBuilder(Func<ChildBuilder, ChildBuilder> modifier = null)
7575
{
76-
return Set(x => x.Child, GetChildBuilder<ChildObject, ChildBuilder>(modifier).Build());
76+
return Set(x => x.Child, GetChildBuilder<ChildObject, ChildBuilder>(modifier));
7777
}
7878

7979
protected override ParentObject BuildObject()

0 commit comments

Comments
 (0)