Skip to content

Commit 570f1d3

Browse files
committed
Introduced MyTested.AspNetCore.Mvc.TempData (#125)
1 parent c5647a4 commit 570f1d3

File tree

46 files changed

+20130
-214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+20130
-214
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Vie
102102
EndProject
103103
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ViewActionResults.Test", "test\MyTested.AspNetCore.Mvc.ViewActionResults.Test\MyTested.AspNetCore.Mvc.ViewActionResults.Test.xproj", "{0FE597F3-3495-440A-A16C-AC1F01CC7C89}"
104104
EndProject
105+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.TempData", "src\MyTested.AspNetCore.Mvc.TempData\MyTested.AspNetCore.Mvc.TempData.xproj", "{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD}"
106+
EndProject
107+
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}"
108+
EndProject
105109
Global
106110
GlobalSection(SolutionConfigurationPlatforms) = preSolution
107111
Debug|Any CPU = Debug|Any CPU
@@ -276,6 +280,14 @@ Global
276280
{0FE597F3-3495-440A-A16C-AC1F01CC7C89}.Debug|Any CPU.Build.0 = Debug|Any CPU
277281
{0FE597F3-3495-440A-A16C-AC1F01CC7C89}.Release|Any CPU.ActiveCfg = Release|Any CPU
278282
{0FE597F3-3495-440A-A16C-AC1F01CC7C89}.Release|Any CPU.Build.0 = Release|Any CPU
283+
{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
284+
{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
285+
{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
286+
{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD}.Release|Any CPU.Build.0 = Release|Any CPU
287+
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
288+
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Debug|Any CPU.Build.0 = Debug|Any CPU
289+
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Release|Any CPU.ActiveCfg = Release|Any CPU
290+
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75}.Release|Any CPU.Build.0 = Release|Any CPU
279291
EndGlobalSection
280292
GlobalSection(SolutionProperties) = preSolution
281293
HideSolutionNode = FALSE
@@ -325,5 +337,7 @@ Global
325337
{56348217-F55C-4C61-8779-B94C009DC4BE} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
326338
{9050F562-C77C-471E-8968-FAF2ED4E6F28} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
327339
{0FE597F3-3495-440A-A16C-AC1F01CC7C89} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
340+
{64DB9A46-8DC1-4DE6-99EB-939A015DA2AD} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
341+
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
328342
EndGlobalSection
329343
EndGlobal

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This package will include all available assertion methods in your test project.
2020
- `MyTested.AspNetCore.Mvc.Controllers` - contains setup and assertion methods for controllers
2121
- `MyTested.AspNetCore.Mvc.Routing` - contains setup and assertion methods for routes
2222
- `MyTested.AspNetCore.Mvc.Core` - contains setup and assertion methods for MVC core features
23+
- `MyTested.AspNetCore.Mvc.TempData` - contains setup and assertion methods for `ITempDataDictionary`
2324
- `MyTested.AspNetCore.Mvc.ViewActionResults` - contains setup and assertion methods for view action results
2425
- `MyTested.AspNetCore.Mvc.ViewFeatures` - contains setup and assertion methods for MVC view features
2526
- `MyTested.AspNetCore.Mvc.Http` - contains setup and assertion methods for HTTP context, request and response

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,6 +4534,20 @@
45344534
"netstandard1.6/MyTested.AspNetCore.Mvc.Session.dll": {}
45354535
}
45364536
},
4537+
"MyTested.AspNetCore.Mvc.TempData/1.0.0-preview-final": {
4538+
"type": "project",
4539+
"framework": ".NETStandard,Version=v1.6",
4540+
"dependencies": {
4541+
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0",
4542+
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0"
4543+
},
4544+
"compile": {
4545+
"netstandard1.6/MyTested.AspNetCore.Mvc.TempData.dll": {}
4546+
},
4547+
"runtime": {
4548+
"netstandard1.6/MyTested.AspNetCore.Mvc.TempData.dll": {}
4549+
}
4550+
},
45374551
"MyTested.AspNetCore.Mvc.ViewActionResults/1.0.0-preview-final": {
45384552
"type": "project",
45394553
"framework": ".NETStandard,Version=v1.6",
@@ -4554,6 +4568,7 @@
45544568
"dependencies": {
45554569
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0",
45564570
"MyTested.AspNetCore.Mvc.DataAnnotations": "1.0.0",
4571+
"MyTested.AspNetCore.Mvc.TempData": "1.0.0",
45574572
"MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0"
45584573
},
45594574
"compile": {
@@ -6963,6 +6978,20 @@
69636978
"net451/MyTested.AspNetCore.Mvc.Session.dll": {}
69646979
}
69656980
},
6981+
"MyTested.AspNetCore.Mvc.TempData/1.0.0-preview-final": {
6982+
"type": "project",
6983+
"framework": ".NETFramework,Version=v4.5.1",
6984+
"dependencies": {
6985+
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0",
6986+
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0"
6987+
},
6988+
"compile": {
6989+
"net451/MyTested.AspNetCore.Mvc.TempData.dll": {}
6990+
},
6991+
"runtime": {
6992+
"net451/MyTested.AspNetCore.Mvc.TempData.dll": {}
6993+
}
6994+
},
69666995
"MyTested.AspNetCore.Mvc.ViewActionResults/1.0.0-preview-final": {
69676996
"type": "project",
69686997
"framework": ".NETFramework,Version=v4.5.1",
@@ -6983,6 +7012,7 @@
69837012
"dependencies": {
69847013
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0",
69857014
"MyTested.AspNetCore.Mvc.DataAnnotations": "1.0.0",
7015+
"MyTested.AspNetCore.Mvc.TempData": "1.0.0",
69867016
"MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0"
69877017
},
69887018
"compile": {
@@ -13675,6 +13705,11 @@
1367513705
"path": "../../../src/MyTested.AspNetCore.Mvc.Session/project.json",
1367613706
"msbuildProject": "../../../src/MyTested.AspNetCore.Mvc.Session/MyTested.AspNetCore.Mvc.Session.xproj"
1367713707
},
13708+
"MyTested.AspNetCore.Mvc.TempData/1.0.0-preview-final": {
13709+
"type": "project",
13710+
"path": "../../../src/MyTested.AspNetCore.Mvc.TempData/project.json",
13711+
"msbuildProject": "../../../src/MyTested.AspNetCore.Mvc.TempData/MyTested.AspNetCore.Mvc.TempData.xproj"
13712+
},
1367813713
"MyTested.AspNetCore.Mvc.ViewActionResults/1.0.0-preview-final": {
1367913714
"type": "project",
1368013715
"path": "../../../src/MyTested.AspNetCore.Mvc.ViewActionResults/project.json",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
using Utilities;
8+
9+
public abstract class BasePropertyHelper
10+
{
11+
private const string InvalidDelegateErrorMessage = "The {0} property cannot be activated for value of {1} type.";
12+
13+
private static readonly MethodInfo CallPropertyGetterOpenGenericMethod =
14+
typeof(BasePropertyHelper).GetTypeInfo().GetDeclaredMethod(nameof(CallPropertyGetter));
15+
16+
protected BasePropertyHelper(Type type)
17+
{
18+
this.Type = type;
19+
this.Properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
20+
}
21+
22+
protected Type Type { get; private set; }
23+
24+
protected IEnumerable<PropertyInfo> Properties { get; private set; }
25+
26+
protected static Func<object, TResult> MakeFastPropertyGetter<TResult>(PropertyInfo propertyInfo)
27+
{
28+
try
29+
{
30+
var propertyGetMethod = propertyInfo.GetMethod;
31+
32+
var typeInput = propertyGetMethod.DeclaringType;
33+
var typeOutput = propertyGetMethod.ReturnType;
34+
35+
var delegateType = typeof(Func<,>).MakeGenericType(typeInput, typeOutput);
36+
var propertyGetterDelegate = propertyGetMethod.CreateDelegate(delegateType);
37+
38+
var wrapperDelegateMethod = CallPropertyGetterOpenGenericMethod.MakeGenericMethod(typeInput, typeOutput);
39+
var accessorDelegate = wrapperDelegateMethod.CreateDelegate(
40+
typeof(Func<object, TResult>),
41+
propertyGetterDelegate);
42+
43+
return (Func<object, TResult>)accessorDelegate;
44+
}
45+
catch
46+
{
47+
throw new InvalidOperationException(string.Format(InvalidDelegateErrorMessage, propertyInfo.Name, typeof(TResult)));
48+
}
49+
}
50+
51+
protected PropertyInfo FindPropertyWithAttribute<TAttribute>()
52+
where TAttribute : Attribute
53+
{
54+
return this.Properties.FirstOrDefault(pr => pr.GetCustomAttribute(typeof(TAttribute), true) != null);
55+
}
56+
57+
protected void ThrowNewInvalidOperationExceptionIfNull(object value, string propertyName)
58+
{
59+
if (value == null)
60+
{
61+
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.");
62+
}
63+
}
64+
65+
// Called via reflection
66+
private static TValue CallPropertyGetter<TDeclaringType, TValue>(
67+
Func<TDeclaringType, TValue> getter,
68+
object target)
69+
{
70+
return getter((TDeclaringType)target);
71+
}
72+
}
73+
}

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

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,20 @@
22
{
33
using System;
44
using System.Collections.Concurrent;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Reflection;
85
using Microsoft.AspNetCore.Mvc;
9-
using Utilities;
106

11-
public class ControllerPropertyHelper
7+
public class ControllerPropertyHelper : BasePropertyHelper
128
{
13-
private const string InvalidDelegateErrorMessage = "The {0} property cannot be activated for value of {1} type.";
14-
15-
private static readonly MethodInfo CallPropertyGetterOpenGenericMethod =
16-
typeof(ControllerPropertyHelper).GetTypeInfo().GetDeclaredMethod(nameof(CallPropertyGetter));
17-
189
private static readonly ConcurrentDictionary<Type, ControllerPropertyHelper> ControllerPropertiesCache =
1910
new ConcurrentDictionary<Type, ControllerPropertyHelper>();
2011

21-
private readonly Type controllerType;
22-
2312
private Func<object, ControllerContext> controllerContextGetter;
2413
private Func<object, ActionContext> actionContextGetter;
2514

2615
public ControllerPropertyHelper(Type controllerType)
16+
: base (controllerType)
2717
{
28-
this.controllerType = controllerType;
29-
this.Properties = controllerType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
18+
3019
}
3120

3221
public Func<object, ControllerContext> ControllerContextGetter
@@ -55,8 +44,6 @@ public Func<object, ActionContext> ActionContextGetter
5544
}
5645
}
5746

58-
protected IEnumerable<PropertyInfo> Properties { get; private set; }
59-
6047
public static ControllerPropertyHelper GetProperties<TController>()
6148
where TController : class
6249
{
@@ -68,39 +55,6 @@ public static ControllerPropertyHelper GetProperties(Type type)
6855
return ControllerPropertiesCache.GetOrAdd(type, _ => new ControllerPropertyHelper(type));
6956
}
7057

71-
protected static Func<object, TResult> MakeFastPropertyGetter<TResult>(PropertyInfo propertyInfo)
72-
{
73-
try
74-
{
75-
var propertyGetMethod = propertyInfo.GetMethod;
76-
77-
var typeInput = propertyGetMethod.DeclaringType;
78-
var typeOutput = propertyGetMethod.ReturnType;
79-
80-
var delegateType = typeof(Func<,>).MakeGenericType(typeInput, typeOutput);
81-
var propertyGetterDelegate = propertyGetMethod.CreateDelegate(delegateType);
82-
83-
var wrapperDelegateMethod = CallPropertyGetterOpenGenericMethod.MakeGenericMethod(typeInput, typeOutput);
84-
var accessorDelegate = wrapperDelegateMethod.CreateDelegate(
85-
typeof(Func<object, TResult>),
86-
propertyGetterDelegate);
87-
88-
return (Func<object, TResult>)accessorDelegate;
89-
}
90-
catch
91-
{
92-
throw new InvalidOperationException(string.Format(InvalidDelegateErrorMessage, propertyInfo.Name, typeof(TResult)));
93-
}
94-
}
95-
96-
// Called via reflection
97-
private static TValue CallPropertyGetter<TDeclaringType, TValue>(
98-
Func<TDeclaringType, TValue> getter,
99-
object target)
100-
{
101-
return getter((TDeclaringType)target);
102-
}
103-
10458
private void TryCreateControllerContextDelegates()
10559
{
10660
var controllerContextProperty = this.FindPropertyWithAttribute<ControllerContextAttribute>();
@@ -116,19 +70,5 @@ private void TryCreateActionContextDelegates()
11670

11771
this.actionContextGetter = MakeFastPropertyGetter<ActionContext>(actionContextProperty);
11872
}
119-
120-
protected PropertyInfo FindPropertyWithAttribute<TAttribute>()
121-
where TAttribute : Attribute
122-
{
123-
return this.Properties.FirstOrDefault(pr => pr.GetCustomAttribute(typeof(TAttribute), true) != null);
124-
}
125-
126-
protected void ThrowNewInvalidOperationExceptionIfNull(object value, string propertyName)
127-
{
128-
if (value == null)
129-
{
130-
throw new InvalidOperationException($"{propertyName} could not be found on the provided {controllerType.ToFriendlyTypeName()}. The property should be specified manually by providing controller instance or using the specified helper methods.");
131-
}
132-
}
13373
}
13474
}

src/MyTested.AspNetCore.Mvc.Http/Plugins/HttpTestPlugin.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
using System;
44
using Microsoft.Extensions.DependencyInjection;
55

6-
public class HttpTestPlugin : BaseTestPlugin, IServiceRegistrationPlugin
6+
public class HttpTestPlugin : BaseTestPlugin, IDefaultRegistrationPlugin, IServiceRegistrationPlugin
77
{
8+
public long Priority => -9000;
9+
10+
public Action<IServiceCollection> DefaultServiceRegistrationDelegate =>
11+
serviceCollection => serviceCollection
12+
.AddMvcCore()
13+
.AddFormatterMappings()
14+
.AddJsonFormatters();
15+
816
public Action<IServiceCollection> ServiceRegistrationDelegate =>
917
serviceCollection => serviceCollection.AddStringInputFormatter();
1018
}

src/MyTested.AspNetCore.Mvc.Routing/Plugins/RoutingTestPlugin.cs

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

66
public class RoutingTestPlugin : IDefaultRegistrationPlugin, IRoutingServiceRegistrationPlugin
77
{
8-
public long Priority => -9000;
8+
public long Priority => -8000;
99

1010
public Action<IServiceCollection> DefaultServiceRegistrationDelegate =>
1111
serviceCollection => serviceCollection
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)