|
| 1 | +namespace MyTested.AspNetCore.Mvc |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using Builders.Base; |
| 6 | + using Builders.Contracts.Base; |
| 7 | + using Builders.Contracts.Data; |
| 8 | + using Builders.Data; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Contains <see cref="Microsoft.EntityFrameworkCore.DbContext"/> |
| 12 | + /// extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>. |
| 13 | + /// </summary> |
| 14 | + public static class ComponentBuilderEntityFrameworkCoreWithExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Sets initial values to the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> on the tested component. |
| 18 | + /// </summary> |
| 19 | + /// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam> |
| 20 | + /// <param name="builder"> |
| 21 | + /// Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type. |
| 22 | + /// </param> |
| 23 | + /// <param name="entities"> |
| 24 | + /// Initial values to add to the registered <see cref="Microsoft.EntityFrameworkCore.DbContext"/>. |
| 25 | + /// </param> |
| 26 | + /// <returns>The same component builder.</returns> |
| 27 | + public static TBuilder WithData<TBuilder>( |
| 28 | + this IBaseTestBuilderWithComponentBuilder<TBuilder> builder, |
| 29 | + IEnumerable<object> entities) |
| 30 | + where TBuilder : IBaseTestBuilder |
| 31 | + => builder |
| 32 | + .WithData(data => data |
| 33 | + .WithEntities(entities)); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Sets initial values to the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> on the tested component. |
| 37 | + /// </summary> |
| 38 | + /// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam> |
| 39 | + /// <param name="builder"> |
| 40 | + /// Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type. |
| 41 | + /// </param> |
| 42 | + /// <param name="entities"> |
| 43 | + /// Initial values to add to the registered <see cref="Microsoft.EntityFrameworkCore.DbContext"/>. |
| 44 | + /// </param> |
| 45 | + /// <returns>The same component builder.</returns> |
| 46 | + public static TBuilder WithData<TBuilder>( |
| 47 | + this IBaseTestBuilderWithComponentBuilder<TBuilder> builder, |
| 48 | + params object[] entities) |
| 49 | + where TBuilder : IBaseTestBuilder |
| 50 | + => builder |
| 51 | + .WithData(data => data |
| 52 | + .WithEntities(entities)); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Sets initial values to the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> on the tested component. |
| 56 | + /// </summary> |
| 57 | + /// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam> |
| 58 | + /// <param name="builder"> |
| 59 | + /// Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type. |
| 60 | + /// </param> |
| 61 | + /// <param name="dbContextBuilder"> |
| 62 | + /// Action setting the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> by using <see cref="IWithDbContextBuilder"/>. |
| 63 | + /// </param> |
| 64 | + /// <returns>The same component builder.</returns> |
| 65 | + public static TBuilder WithData<TBuilder>( |
| 66 | + this IBaseTestBuilderWithComponentBuilder<TBuilder> builder, |
| 67 | + Action<IWithDbContextBuilder> dbContextBuilder) |
| 68 | + where TBuilder : IBaseTestBuilder |
| 69 | + { |
| 70 | + var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder; |
| 71 | + |
| 72 | + dbContextBuilder(new WithDbContextBuilder(actualBuilder.TestContext)); |
| 73 | + |
| 74 | + return actualBuilder.Builder; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments