Skip to content

Commit 818ecad

Browse files
committed
Added tests for all ViewComponent plugins (closes #66)
1 parent f8f47da commit 818ecad

File tree

38 files changed

+832
-241
lines changed

38 files changed

+832
-241
lines changed

samples/MusicStore/MusicStore.Test/project.lock.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4549,7 +4549,7 @@
45494549
"framework": ".NETStandard,Version=v1.6",
45504550
"dependencies": {
45514551
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0",
4552-
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0"
4552+
"MyTested.AspNetCore.Mvc.Abstractions": "1.0.0"
45534553
},
45544554
"compile": {
45554555
"netstandard1.6/MyTested.AspNetCore.Mvc.TempData.dll": {}
@@ -7050,7 +7050,7 @@
70507050
"framework": ".NETFramework,Version=v4.5.1",
70517051
"dependencies": {
70527052
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0",
7053-
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0"
7053+
"MyTested.AspNetCore.Mvc.Abstractions": "1.0.0"
70547054
},
70557055
"compile": {
70567056
"net451/MyTested.AspNetCore.Mvc.TempData.dll": {}

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/PropertyHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected void ThrowNewInvalidOperationExceptionIfNull(object value, string prop
8080
{
8181
if (value == null)
8282
{
83-
throw new InvalidOperationException($"{propertyName} could not be found on the provided {this.Type.ToFriendlyTypeName()}. The property should be specified manually by providing controller instance or using the specified helper methods.");
83+
throw new InvalidOperationException($"{propertyName} could not be found on the provided {this.Type.ToFriendlyTypeName()}. The property should be specified manually by providing component instance or using the specified helper methods.");
8484
}
8585
}
8686

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestContexts/ComponentTestContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public abstract class ComponentTestContext : HttpTestContext
1818
private LambdaExpression methodCall;
1919
private IEnumerable<object> methodAttributes;
2020
private object model;
21-
21+
2222
public object Component
2323
{
2424
get
@@ -141,7 +141,7 @@ public object Model
141141
public Action ComponentPreparationDelegate { get; set; }
142142

143143
public Action PreMethodInvocationDelegate { get; set; }
144-
144+
145145
public TComponent ComponentAs<TComponent>()
146146
where TComponent : class => this.Component as TComponent;
147147

src/MyTested.AspNetCore.Mvc.Controllers/Internal/Controllers/ControllerPropertyHelper.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ public class ControllerPropertyHelper : PropertyHelper
99
private static readonly ConcurrentDictionary<Type, ControllerPropertyHelper> ControllerPropertiesCache =
1010
new ConcurrentDictionary<Type, ControllerPropertyHelper>();
1111

12-
private Func<object, ControllerContext> controllerContextGetter; // DO I NEED THESE???
13-
private Func<object, ActionContext> actionContextGetter; // DO I NEED THESE?
12+
private Func<object, ControllerContext> controllerContextGetter;
13+
private Func<object, ActionContext> actionContextGetter;
1414

1515
public ControllerPropertyHelper(Type controllerType)
1616
: base (controllerType)
1717
{
18-
1918
}
2019

2120
public Func<object, ControllerContext> ControllerContextGetter

src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/TempDataTestBuilder.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010
public class TempDataTestBuilder : BaseDataProviderWithStringKeyTestBuilder<IAndTempDataTestBuilder>, IAndTempDataTestBuilder
1111
{
1212
internal const string TempDataName = "temp data";
13-
14-
private readonly ControllerTestContext testContext;
15-
13+
1614
/// <summary>
1715
/// Initializes a new instance of the <see cref="TempDataTestBuilder"/> class.
1816
/// </summary>
19-
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
20-
public TempDataTestBuilder(ControllerTestContext testContext)
17+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
18+
public TempDataTestBuilder(ComponentTestContext testContext)
2119
: base(testContext, TempDataName)
2220
{
23-
this.testContext = testContext;
2421
}
2522

2623
/// <summary>
@@ -36,6 +33,6 @@ public TempDataTestBuilder(ControllerTestContext testContext)
3633
/// When overridden in derived class provides a way to built the data provider as <see cref="IDictionary{TKey,TValue}"/>.
3734
/// </summary>
3835
/// <returns>Data provider as <see cref="IDictionary{TKey,TValue}"/>.</returns>
39-
protected override IDictionary<string, object> GetDataProvider() => this.testContext.GetTempData();
36+
protected override IDictionary<string, object> GetDataProvider() => this.TestContext.GetTempData();
4037
}
4138
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using System;
4+
using Builders.Contracts.Base;
5+
using Builders.Contracts.Data;
6+
using Builders.Base;
7+
using Builders.Data;
8+
using Internal.TestContexts;
9+
10+
/// <summary>
11+
/// Contains <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary"/> extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>.
12+
/// </summary>
13+
public static class ComponentBuilderTempDataExtensions
14+
{
15+
/// <summary>
16+
/// Sets initial values to the <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary"/> on the tested component.
17+
/// </summary>
18+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
19+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
20+
/// <param name="tempDataBuilder">Action setting the <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary"/> values by using <see cref="ITempDataBuilder"/>.</param>
21+
/// <returns>The same component builder.</returns>
22+
public static TBuilder WithTempData<TBuilder>(
23+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
24+
Action<ITempDataBuilder> tempDataBuilder)
25+
where TBuilder : IBaseTestBuilder
26+
{
27+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
28+
29+
actualBuilder.TestContext.ComponentPreparationDelegate += () =>
30+
{
31+
tempDataBuilder(new TempDataBuilder(actualBuilder.TestContext.GetTempData()));
32+
};
33+
34+
return actualBuilder.Builder;
35+
}
36+
}
37+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary"/> extension methods for <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/>.
13+
/// </summary>
14+
public static class ComponentShouldHaveTestBuilderTempDataExtensions
15+
{
16+
/// <summary>
17+
/// Tests whether the component does not set any <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary"/> 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 NoTempData<TBuilder>(this IBaseTestBuilderWithComponentShouldHaveTestBuilder<TBuilder> builder)
23+
where TBuilder : IBaseTestBuilder
24+
{
25+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
26+
27+
if (actualBuilder.TestContext.GetTempData().Count > 0)
28+
{
29+
DataProviderValidator.ThrowNewDataProviderAssertionExceptionWithNoEntries(
30+
actualBuilder.TestContext,
31+
TempDataTestBuilder.TempDataName);
32+
}
33+
34+
return actualBuilder.Builder;
35+
}
36+
/// <summary>
37+
/// Tests whether the component sets entries in the <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary"/>.
38+
/// </summary>
39+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
40+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/> type.</param>
41+
/// <param name="withNumberOfEntries">Expected number of <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary"/> entries.
42+
/// If default null is provided, the test builder checks only if any entries are found.</param>
43+
/// <returns>The same component should have test builder.</returns>
44+
public static TBuilder TempData<TBuilder>(
45+
this IBaseTestBuilderWithComponentShouldHaveTestBuilder<TBuilder> builder,
46+
int? withNumberOfEntries = null)
47+
where TBuilder : IBaseTestBuilder
48+
{
49+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
50+
51+
DataProviderValidator.ValidateDataProviderNumberOfEntries(
52+
actualBuilder.TestContext,
53+
TempDataTestBuilder.TempDataName,
54+
withNumberOfEntries,
55+
actualBuilder.TestContext.GetTempData().Count);
56+
57+
return actualBuilder.Builder;
58+
}
59+
/// <summary>
60+
/// Tests whether the component sets specific entries in the <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary"/>.
61+
/// </summary>
62+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
63+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentShouldHaveTestBuilder{TBuilder}"/> type.</param>
64+
/// <param name="tempDataTestBuilder">Builder for testing specific <see cref="Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary"/> entries.</param>
65+
/// <returns>The same component should have test builder.</returns>
66+
public static TBuilder TempData<TBuilder>(
67+
this IBaseTestBuilderWithComponentShouldHaveTestBuilder<TBuilder> builder,
68+
Action<ITempDataTestBuilder> tempDataTestBuilder)
69+
where TBuilder : IBaseTestBuilder
70+
{
71+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
72+
73+
tempDataTestBuilder(new TempDataTestBuilder(actualBuilder.TestContext));
74+
75+
return actualBuilder.Builder;
76+
}
77+
}
78+
}

src/MyTested.AspNetCore.Mvc.TempData/ControllerBuilderTempDataExtensions.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
namespace MyTested.AspNetCore.Mvc.Internal.Controllers
1+
namespace MyTested.AspNetCore.Mvc.Internal
22
{
33
using System;
44
using System.Collections.Concurrent;
55
using System.Linq;
66
using System.Reflection;
77
using Microsoft.AspNetCore.Mvc.ViewFeatures;
88

9-
public class TempDataControllerPropertyHelper : ControllerPropertyHelper
9+
public class TempDataPropertyHelper : PropertyHelper
1010
{
11-
private static readonly ConcurrentDictionary<Type, TempDataControllerPropertyHelper> TempDataControllerPropertiesCache =
12-
new ConcurrentDictionary<Type, TempDataControllerPropertyHelper>();
11+
private static readonly ConcurrentDictionary<Type, TempDataPropertyHelper> TempDataPropertiesCache =
12+
new ConcurrentDictionary<Type, TempDataPropertyHelper>();
1313

1414
private static readonly TypeInfo TypeOfTempDataDictionary = typeof(ITempDataDictionary).GetTypeInfo();
1515

1616
private Func<object, ITempDataDictionary> tempDataGetter;
1717

18-
public TempDataControllerPropertyHelper(Type controllerType)
19-
: base(controllerType)
18+
public TempDataPropertyHelper(Type type)
19+
: base(type)
2020
{
2121
}
2222

@@ -33,15 +33,15 @@ public Func<object, ITempDataDictionary> TempDataGetter
3333
}
3434
}
3535

36-
public static TempDataControllerPropertyHelper GetTempDataProperties<TController>()
36+
public static TempDataPropertyHelper GetTempDataProperties<TController>()
3737
where TController : class
3838
{
3939
return GetTempDataProperties(typeof(TController));
4040
}
4141

42-
public static TempDataControllerPropertyHelper GetTempDataProperties(Type type)
42+
public static TempDataPropertyHelper GetTempDataProperties(Type type)
4343
{
44-
return TempDataControllerPropertiesCache.GetOrAdd(type, _ => new TempDataControllerPropertyHelper(type));
44+
return TempDataPropertiesCache.GetOrAdd(type, _ => new TempDataPropertyHelper(type));
4545
}
4646

4747
private void TryCreateTempDataGetterDelegate()
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
namespace MyTested.AspNetCore.Mvc.Internal.TestContexts
22
{
3-
using Controllers;
43
using Microsoft.AspNetCore.Mvc;
54
using Microsoft.AspNetCore.Mvc.ViewFeatures;
65

7-
public static class ControllerTestContextTempDataExtensions
6+
public static class ComponentTestContextTempDataExtensions
87
{
9-
public static ITempDataDictionary GetTempData(this ControllerTestContext testContext)
8+
public static ITempDataDictionary GetTempData(this ComponentTestContext testContext)
109
=> testContext.ComponentAs<Controller>()?.TempData
11-
?? TempDataControllerPropertyHelper
10+
?? testContext.ComponentAs<ViewComponent>()?.ViewContext?.TempData
11+
?? TempDataPropertyHelper
1212
.GetTempDataProperties(testContext.Component.GetType())
1313
.TempDataGetter(testContext.Component);
1414
}

0 commit comments

Comments
 (0)