Skip to content

Commit 159ea3e

Browse files
committed
Added MyTested.AspNetCore.Mvc.ViewData (#125)
1 parent 570f1d3 commit 159ea3e

20 files changed

+8311
-234
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Tem
106106
EndProject
107107
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.TempData.Test", "test\MyTested.AspNetCore.Mvc.TempData.Test\MyTested.AspNetCore.Mvc.TempData.Test.xproj", "{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}"
108108
EndProject
109+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ViewData", "src\MyTested.AspNetCore.Mvc.ViewData\MyTested.AspNetCore.Mvc.ViewData.xproj", "{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC}"
110+
EndProject
109111
Global
110112
GlobalSection(SolutionConfigurationPlatforms) = preSolution
111113
Debug|Any CPU = Debug|Any CPU
@@ -288,6 +290,10 @@ Global
288290
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Debug|Any CPU.Build.0 = Debug|Any CPU
289291
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Release|Any CPU.ActiveCfg = Release|Any CPU
290292
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Release|Any CPU.Build.0 = Release|Any CPU
293+
{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
294+
{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
295+
{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
296+
{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC}.Release|Any CPU.Build.0 = Release|Any CPU
291297
EndGlobalSection
292298
GlobalSection(SolutionProperties) = preSolution
293299
HideSolutionNode = FALSE
@@ -339,5 +345,6 @@ Global
339345
{0FE597F3-3495-440A-A16C-AC1F01CC7C89} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
340346
{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
341347
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
348+
{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
342349
EndGlobalSection
343350
EndGlobal

src/MyTested.AspNetCore.Mvc.TempData/Internal/Controllers/TempDataControllerPropertyHelper.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
public class TempDataControllerPropertyHelper : ControllerPropertyHelper
1010
{
11-
private static readonly ConcurrentDictionary<Type, TempDataControllerPropertyHelper> ControllerPropertiesCache =
11+
private static readonly ConcurrentDictionary<Type, TempDataControllerPropertyHelper> TempDataControllerPropertiesCache =
1212
new ConcurrentDictionary<Type, TempDataControllerPropertyHelper>();
1313

14+
private static readonly TypeInfo TypeOfTempDataDictionary = typeof(ITempDataDictionary).GetTypeInfo();
15+
1416
private Func<object, ITempDataDictionary> tempDataGetter;
1517

1618
public TempDataControllerPropertyHelper(Type controllerType)
@@ -39,12 +41,12 @@ public static TempDataControllerPropertyHelper GetTempDataProperties<TController
3941

4042
public static TempDataControllerPropertyHelper GetTempDataProperties(Type type)
4143
{
42-
return ControllerPropertiesCache.GetOrAdd(type, _ => new TempDataControllerPropertyHelper(type));
44+
return TempDataControllerPropertiesCache.GetOrAdd(type, _ => new TempDataControllerPropertyHelper(type));
4345
}
4446

4547
private void TryCreateTempDataGetterDelegate()
4648
{
47-
var tempDataProperty = this.Properties.FirstOrDefault(pr => typeof(ITempDataDictionary).GetTypeInfo().IsAssignableFrom(pr.PropertyType));
49+
var tempDataProperty = this.Properties.FirstOrDefault(pr => TypeOfTempDataDictionary.IsAssignableFrom(pr.PropertyType));
4850
this.ThrowNewInvalidOperationExceptionIfNull(tempDataProperty, nameof(TempDataDictionary));
4951

5052
this.tempDataGetter = MakeFastPropertyGetter<ITempDataDictionary>(tempDataProperty);

src/MyTested.AspNetCore.Mvc.TempData/Internal/TestContexts/ControllerTestContextTempDataExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public static class ControllerTestContextTempDataExtensions
88
{
9-
public static ITempDataDictionary GetTempData(this ComponentTestContext testContext)
9+
public static ITempDataDictionary GetTempData(this ControllerTestContext testContext)
1010
=> testContext.ComponentAs<Controller>()?.TempData
1111
?? TempDataControllerPropertyHelper
1212
.GetTempDataProperties(testContext.Component.GetType())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
22
{
33
/// <summary>
4-
/// Used for adding AndAlso() method to the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> tests.
4+
/// Used for adding AndAlso() method to the dynamic view bag tests.
55
/// </summary>
66
public interface IAndViewBagTestBuilder : IViewBagTestBuilder
77
{
88
/// <summary>
9-
/// AndAlso method for better readability when testing <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/>.
9+
/// AndAlso method for better readability when testing dynamic view bag.
1010
/// </summary>
1111
/// <returns>The same <see cref="IViewBagTestBuilder"/>.</returns>
1212
IViewBagTestBuilder AndAlso();
File renamed without changes.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,57 @@
33
using System.Collections.Generic;
44

55
/// <summary>
6-
/// Used for testing <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/>.
6+
/// Used for testing dynamic view bag.
77
/// </summary>
88
public interface IViewBagTestBuilder
99
{
1010
/// <summary>
11-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains entry with the provided key.
11+
/// Tests whether the dynamic view bag contains entry with the provided key.
1212
/// </summary>
1313
/// <param name="key">Key of the view bag entry.</param>
1414
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
1515
IAndViewBagTestBuilder ContainingEntryWithKey(string key);
1616

1717
/// <summary>
18-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains entry with the provided value.
18+
/// Tests whether the dynamic view bag contains entry with the provided value.
1919
/// </summary>
2020
/// <typeparam name="TEntry">Type of the view bag entry value.</typeparam>
2121
/// <param name="value">Value of the view bag entry.</param>
2222
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
2323
IAndViewBagTestBuilder ContainingEntryWithValue<TEntry>(TEntry value);
2424

2525
/// <summary>
26-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains entry with value of the provided type.
26+
/// Tests whether the dynamic view bag contains entry with value of the provided type.
2727
/// </summary>
2828
/// <typeparam name="TEntry">Type of the view bag entry value.</typeparam>
2929
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
3030
IAndViewBagTestBuilder ContainingEntryOfType<TEntry>();
3131

3232
/// <summary>
33-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains entry with value of the provided type and the given key.
33+
/// Tests whether the dynamic view bag contains entry with value of the provided type and the given key.
3434
/// </summary>
3535
/// <typeparam name="TEntry">Type of the view bag entry value.</typeparam>
3636
/// <param name="key">Key of the view bag entry.</param>
3737
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
3838
IAndViewBagTestBuilder ContainingEntryOfType<TEntry>(string key);
3939

4040
/// <summary>
41-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains entry with the provided key and corresponding value.
41+
/// Tests whether the dynamic view bag contains entry with the provided key and corresponding value.
4242
/// </summary>
4343
/// <param name="key">Key of the view bag entry.</param>
4444
/// <param name="value">Value of the view bag entry.</param>
4545
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
4646
IAndViewBagTestBuilder ContainingEntry(string key, object value);
4747

4848
/// <summary>
49-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains the provided entries.
49+
/// Tests whether the dynamic view bag contains the provided entries.
5050
/// </summary>
5151
/// <param name="entries">Anonymous object of view bag entries.</param>
5252
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
5353
IAndViewBagTestBuilder ContainingEntries(object entries);
5454

5555
/// <summary>
56-
/// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/> contains the provided entries.
56+
/// Tests whether the dynamic view bag contains the provided entries.
5757
/// </summary>
5858
/// <param name="entries">Dictionary of view bag entries.</param>
5959
/// <returns>The same <see cref="IAndViewBagTestBuilder"/>.</returns>
File renamed without changes.

src/MyTested.AspNetCore.Mvc.ViewFeatures/Builders/Data/ViewBagTestBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.ViewData/Builders/Data/ViewBagTestBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
using Internal.TestContexts;
66

77
/// <summary>
8-
/// Used for testing <see cref="Microsoft.AspNetCore.Mvc.Controller.ViewBag"/>.
8+
/// Used for testing dynamic view bag.
99
/// </summary>
1010
public class ViewBagTestBuilder : BaseDataProviderWithStringKeyTestBuilder<IAndViewBagTestBuilder>, IAndViewBagTestBuilder
1111
{
1212
internal const string ViewBagName = "view bag";
1313

14-
private readonly ControllerTestContext testContext;
14+
private readonly ComponentTestContext testContext;
1515

1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="ViewBagTestBuilder"/> class.
1818
/// </summary>
19-
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
20-
public ViewBagTestBuilder(ControllerTestContext testContext)
19+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
20+
public ViewBagTestBuilder(ComponentTestContext testContext)
2121
: base(testContext, ViewBagName)
2222
{
2323
this.testContext = testContext;

src/MyTested.AspNetCore.Mvc.ViewFeatures/Builders/Data/ViewDataTestBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.ViewData/Builders/Data/ViewDataTestBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public class ViewDataTestBuilder : BaseDataProviderWithStringKeyTestBuilder<IAnd
1111
{
1212
internal const string ViewDataName = "view data";
1313

14-
private readonly ControllerTestContext testContext;
14+
private readonly ComponentTestContext testContext;
1515

1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="ViewDataTestBuilder"/> class.
1818
/// </summary>
19-
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
20-
public ViewDataTestBuilder(ControllerTestContext testContext)
19+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
20+
public ViewDataTestBuilder(ComponentTestContext testContext)
2121
: base(testContext, ViewDataName)
2222
{
2323
this.testContext = testContext;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)