|
1 |
| -using TestStack.Dossier.Factories; |
| 1 | +using Ploeh.AutoFixture.Kernel; |
| 2 | +using TestStack.Dossier.Factories; |
| 3 | +using TestStack.Dossier.Lists; |
2 | 4 |
|
3 | 5 | namespace TestStack.Dossier
|
4 | 6 | {
|
5 | 7 | /// <summary>
|
6 |
| - /// A stand-alone class for building objects on the fly. |
| 8 | + /// A generic Test Data Builder implementation for building objects on the fly. |
| 9 | + /// By default |
7 | 10 | /// </summary>
|
8 | 11 | /// <typeparam name="T">The type of object this class generates.</typeparam>
|
9 | 12 | public class Builder<T> : TestDataBuilder<T, Builder<T>>
|
10 | 13 | where T : class
|
11 | 14 | {
|
| 15 | + /// <summary> |
| 16 | + /// Default constructor. |
| 17 | + /// </summary> |
| 18 | + public Builder() |
| 19 | + { |
| 20 | + Factory = new AllPropertiesFactory(); |
| 21 | + } |
| 22 | + |
| 23 | + private IFactory Factory { get; set; } |
| 24 | + |
| 25 | + internal Builder<T> SetFactory(IFactory factory) |
| 26 | + { |
| 27 | + Factory = factory; |
| 28 | + return this; |
| 29 | + } |
| 30 | + |
12 | 31 | /// <summary>
|
13 | 32 | /// Initialises a new Builder.
|
14 | 33 | /// </summary>
|
| 34 | + /// <param name="factory">The type of factory to use to construct the object, uses <see cref="AllPropertiesFactory"/> by default</param> |
15 | 35 | /// <returns>Returns a new instance of a Builder for the type of T</returns>
|
16 | 36 | public static Builder<T> CreateNew(IFactory factory = null)
|
17 | 37 | {
|
18 |
| - if (factory == null) |
| 38 | + return new Builder<T> |
19 | 39 | {
|
20 |
| - factory = new AllPropertiesFactory(); |
21 |
| - } |
22 |
| - return new Builder<T> {Factory = factory}; |
| 40 | + Factory = factory ?? new AllPropertiesFactory() |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Creates an list builder expression that allows you to create a list of entities. |
| 46 | + /// You can call .First(x), .Last(x), etc. methods followed by chained builder method calls. |
| 47 | + /// When you are done call .BuildList() to get the list of entities. |
| 48 | + /// This override allows you to specify the factory that is used to construct the objects, |
| 49 | + /// the default implementation uses the <see cref="AllPropertiesFactory"/>. |
| 50 | + /// </summary> |
| 51 | + /// <param name="size">The size of list</param> |
| 52 | + /// <param name="factory">The factory to use to construct the object</param> |
| 53 | + /// <returns>The list builder for a list of {TBuilder} of the specified size</returns> |
| 54 | + public static ListBuilder<T, Builder<T>> CreateListOfSize(int size, IFactory factory) |
| 55 | + { |
| 56 | + var lb = new ListBuilder<T, Builder<T>>(size); |
| 57 | + lb.All().With(b => b.SetFactory(factory)); |
| 58 | + return lb; |
23 | 59 | }
|
| 60 | + |
| 61 | + /// <inheritdoc /> |
| 62 | + protected override T BuildObject() |
| 63 | + { |
| 64 | + return Factory.BuildObject(this); |
| 65 | + } |
24 | 66 | }
|
25 | 67 | }
|
0 commit comments