| | | 1 | | namespace ZhEaIsNsAaBn.IntegrationTestingBases; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Base class for fluent test-data builders. Derive one per entity, set defaults in <see cref="CreateDefault"/>, |
| | | 5 | | /// and add <c>With…</c> methods that change <see cref="Entity"/>. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <typeparam name="T">The entity type being built.</typeparam> |
| | | 8 | | public abstract class TestDataBuilder< T > |
| | | 9 | | where T : class |
| | | 10 | | { |
| | | 11 | | /// <summary>Creates the builder and its <see cref="Entity"/> from <see cref="CreateDefault"/>.</summary> |
| | 6 | 12 | | protected TestDataBuilder() => Entity = CreateDefault(); |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | /// <summary>The entity being configured.</summary> |
| | | 16 | | protected T Entity { get; set; } |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | /// <summary>Creates a new entity with default values. Called by the constructor and by <see cref="BuildMany"/>.</su |
| | | 20 | | /// <returns>A new entity.</returns> |
| | | 21 | | protected abstract T CreateDefault(); |
| | | 22 | | |
| | | 23 | | /// <summary>Returns the configured entity.</summary> |
| | | 24 | | /// <returns><see cref="Entity"/>.</returns> |
| | 3 | 25 | | public virtual T Build() => Entity; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Creates <paramref name="count"/> new entities from <see cref="CreateDefault"/>. |
| | | 29 | | /// <c>With…</c> changes on this builder are <b>not</b> applied to them. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="count">How many entities to create.</param> |
| | | 32 | | /// <returns>The new entities.</returns> |
| | | 33 | | public virtual List<T> BuildMany( int count ) |
| | | 34 | | { |
| | 3 | 35 | | var entities = new List<T>( count ); |
| | | 36 | | |
| | 33 | 37 | | for ( var i = 0; i < count; i++ ) { entities.Add( CreateDefault() ); } |
| | | 38 | | |
| | | 39 | | |
| | 3 | 40 | | return entities; |
| | | 41 | | } |
| | | 42 | | } |