|
| 1 | +namespace MyTested.AspNetCore.Mvc |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using Builders.Base; |
| 5 | + using Builders.Contracts.Base; |
| 6 | + using Builders.Contracts.Data; |
| 7 | + using Builders.Data; |
| 8 | + using Internal.TestContexts; |
| 9 | + using Utilities.Validators; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Contains dynamic view bag extension methods for <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/>. |
| 13 | + /// </summary> |
| 14 | + public static class ComponentShouldHaveTestBuilderViewBagExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Tests whether the component does not set any dynamic view bag entries. |
| 18 | + /// </summary> |
| 19 | + /// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam> |
| 20 | + /// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/> type.</param> |
| 21 | + /// <returns>The same component should have test builder.</returns> |
| 22 | + public static TBuilder NoViewBag<TBuilder>(this IBaseTestBuilderWithComponentShouldHaveTestBuilder<TBuilder> builder) |
| 23 | + where TBuilder : IBaseTestBuilder |
| 24 | + { |
| 25 | + var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder; |
| 26 | + |
| 27 | + if (actualBuilder.TestContext.GetViewData().Count > 0) |
| 28 | + { |
| 29 | + DataProviderValidator.ThrowNewDataProviderAssertionExceptionWithNoEntries( |
| 30 | + actualBuilder.TestContext, |
| 31 | + ViewBagTestBuilder.ViewBagName); |
| 32 | + } |
| 33 | + |
| 34 | + return actualBuilder.Builder; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Tests whether the component sets entries in the dynamic view bag. |
| 39 | + /// </summary> |
| 40 | + /// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam> |
| 41 | + /// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/> type.</param> |
| 42 | + /// <param name="withNumberOfEntries">Expected number of dynamic view bag entries. |
| 43 | + /// If default null is provided, the test builder checks only if any entries are found.</param> |
| 44 | + /// <returns>The same component should have test builder.</returns> |
| 45 | + public static TBuilder ViewBag<TBuilder>( |
| 46 | + this IBaseTestBuilderWithComponentShouldHaveTestBuilder<TBuilder> builder, |
| 47 | + int? withNumberOfEntries = null) |
| 48 | + where TBuilder : IBaseTestBuilder |
| 49 | + { |
| 50 | + var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder; |
| 51 | + |
| 52 | + DataProviderValidator.ValidateDataProviderNumberOfEntries( |
| 53 | + actualBuilder.TestContext, |
| 54 | + ViewBagTestBuilder.ViewBagName, |
| 55 | + withNumberOfEntries, |
| 56 | + actualBuilder.TestContext.GetViewData().Count); |
| 57 | + |
| 58 | + return actualBuilder.Builder; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Tests whether the component sets specific entries in the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/>. |
| 63 | + /// </summary> |
| 64 | + /// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam> |
| 65 | + /// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/> type.</param> |
| 66 | + /// <param name="viewBagTestBuilder">Builder for testing specific dynamic view bag entries.</param> |
| 67 | + /// <returns>The same component should have test builder.</returns> |
| 68 | + public static TBuilder ViewBag<TBuilder>( |
| 69 | + this IBaseTestBuilderWithComponentShouldHaveTestBuilder<TBuilder> builder, |
| 70 | + Action<IViewBagTestBuilder> viewBagTestBuilder) |
| 71 | + where TBuilder : IBaseTestBuilder |
| 72 | + { |
| 73 | + var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder; |
| 74 | + |
| 75 | + viewBagTestBuilder(new ViewBagTestBuilder(actualBuilder.TestContext)); |
| 76 | + |
| 77 | + return actualBuilder.Builder; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments