Skip to content

Commit f8f47da

Browse files
committed
Added ViewComponent tests for routing and session (#66)
1 parent 5d0b2bd commit f8f47da

File tree

29 files changed

+755
-39
lines changed

29 files changed

+755
-39
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ This package will include all available assertion methods in your test project,
2727
- `MyTested.AspNetCore.Mvc.ViewFeatures` - contains setup and assertion methods for MVC view features
2828
- `MyTested.AspNetCore.Mvc.Http` - contains setup and assertion methods for HTTP context, request and response
2929
- `MyTested.AspNetCore.Mvc.Authentication` - contains setup methods for `ClaimsPrincipal`
30-
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for `ModelState` validations
30+
- `MyTested.AspNetCore.Mvc.ModelState` - contains setup and assertion methods for `ModelStateDictionary` validations
31+
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for data annotation validations
3132
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - contains setup and assertion methods for `DbContext`
3233
- `MyTested.AspNetCore.Mvc.DependencyInjection` - contains setup methods for dependency injection services
3334
- `MyTested.AspNetCore.Mvc.Caching` - contains setup and assertion methods for `IMemoryCache`

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestContexts/ActionTestContext{TComponentContext}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public override RouteData RouteData
2525
return routeData;
2626
}
2727

28-
if (this.expressionRouteData == null && this.MethodCall != null)
28+
if (this.expressionRouteData == null && this.RouteDataMethodCall != null)
2929
{
30-
this.expressionRouteData = RouteExpressionParser.ResolveRouteData(TestApplication.Router, this.MethodCall);
30+
this.expressionRouteData = RouteExpressionParser.ResolveRouteData(TestApplication.Router, this.RouteDataMethodCall);
3131
}
3232

3333
return this.expressionRouteData;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Internal.TestContexts
22
{
3+
using System.Linq.Expressions;
34
using Application;
45
using Http;
56
using Microsoft.AspNetCore.Http;
@@ -59,7 +60,9 @@ public virtual RouteData RouteData
5960
this.routeData = value;
6061
}
6162
}
62-
63+
64+
public virtual LambdaExpression RouteDataMethodCall => null;
65+
6366
public ISession Session => this.HttpContext.Session;
6467

6568
public abstract string ExceptionMessagePrefix { get; }

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Extensions/RouteValueDictionaryExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Utilities.Extensions
22
{
33
using Microsoft.AspNetCore.Routing;
4-
using System.Collections.Generic;
54

65
public static class RouteValueDictionaryExtensions
76
{
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace MyTested.AspNetCore.Mvc.Internal.TestContexts
22
{
3+
using System.Linq.Expressions;
34
using Controllers;
45
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.AspNetCore.Mvc.ModelBinding;
66
using Utilities.Extensions;
77

88
public class ControllerTestContext : ActionTestContext<ControllerContext>
@@ -11,5 +11,7 @@ public class ControllerTestContext : ActionTestContext<ControllerContext>
1111

1212
protected override ControllerContext DefaultComponentContext
1313
=> ControllerContextMock.Default(this);
14+
15+
public override LambdaExpression RouteDataMethodCall => this.MethodCall;
1416
}
1517
}
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

5-
// General Information about an assembly is controlled through the following
6-
// set of attributes. Change these attribute values to modify the information
7-
// associated with an assembly.
8-
[assembly: AssemblyConfiguration("")]
9-
[assembly: AssemblyCompany("")]
104
[assembly: AssemblyProduct("MyTested.AspNetCore.Mvc.ModelState")]
11-
[assembly: AssemblyTrademark("")]
12-
13-
// Setting ComVisible to false makes the types in this assembly not visible
14-
// to COM components. If you need to access a type in this assembly from
15-
// COM, set the ComVisible attribute to true on that type.
165
[assembly: ComVisible(false)]
17-
18-
// The following GUID is for the ID of the typelib if this project is exposed to COM
19-
[assembly: Guid("13a781f9-8766-4016-b72e-fa7e61dcf85f")]

src/MyTested.AspNetCore.Mvc.Routing/ComponentBuilderRoutingExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Builders.Contracts.Base;
55
using Internal.Application;
66
using Internal.Routing;
7+
using Microsoft.AspNetCore.Routing;
78
using Utilities.Extensions;
89

910
/// <summary>
@@ -41,7 +42,20 @@ public static TBuilder WithRouteData<TBuilder>(
4142
actualBuilder.TestContext.PreMethodInvocationDelegate += () =>
4243
{
4344
var testContext = actualBuilder.TestContext;
44-
testContext.RouteData = RouteExpressionParser.ResolveRouteData(TestApplication.Router, testContext.MethodCall);
45+
46+
if (testContext.RouteDataMethodCall != null)
47+
{
48+
testContext.RouteData = RouteExpressionParser.ResolveRouteData(
49+
TestApplication.Router,
50+
testContext.RouteDataMethodCall);
51+
}
52+
53+
if (testContext.RouteData == null)
54+
{
55+
testContext.RouteData = new RouteData();
56+
testContext.RouteData.Routers.Add(TestApplication.Router);
57+
}
58+
4559
testContext.RouteData.Values.Add(additionalRouteValues);
4660
};
4761

src/MyTested.AspNetCore.Mvc.ViewComponents/Internal/TestContexts/ViewComponentTestContext.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Mvc.ModelBinding;
55
using Microsoft.AspNetCore.Mvc.Rendering;
66
using Microsoft.AspNetCore.Mvc.ViewComponents;
7+
using Microsoft.AspNetCore.Routing;
78
using Utilities.Extensions;
89
using Utilities.Validators;
910
using ViewComponents;
@@ -15,10 +16,25 @@ public class ViewComponentTestContext : ActionTestContext<ViewContext>
1516
public override string ExceptionMessagePrefix => $"When invoking {this.Component.GetName()} expected";
1617

1718
public override ModelStateDictionary ModelState => this.viewComponentContext.ViewData.ModelState;
18-
19+
1920
protected override ViewContext DefaultComponentContext
2021
=> ViewContextMock.Default(this);
2122

23+
public override RouteData RouteData
24+
{
25+
get
26+
{
27+
return base.RouteData;
28+
}
29+
30+
set
31+
{
32+
CommonValidator.CheckForNullReference(value, nameof(RouteData));
33+
this.ViewComponentContext.ViewContext.RouteData = value;
34+
base.RouteData = value;
35+
}
36+
}
37+
2238
public ViewComponentContext ViewComponentContext
2339
{
2440
get

src/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ MyTested.AspNetCore.Mvc Packages
1414
- `MyTested.AspNetCore.Mvc.ViewFeatures` - contains setup and assertion methods for MVC view features
1515
- `MyTested.AspNetCore.Mvc.Http` - contains setup and assertion methods for HTTP context, request and response
1616
- `MyTested.AspNetCore.Mvc.Authentication` - contains setup methods for `ClaimsPrincipal`
17-
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for `ModelState` validations
17+
- `MyTested.AspNetCore.Mvc.ModelState` - contains setup and assertion methods for `ModelStateDictionary` validations
18+
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for data annotation validations
1819
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - contains setup and assertion methods for `DbContext`
1920
- `MyTested.AspNetCore.Mvc.DependencyInjection` - contains setup methods for dependency injection services
2021
- `MyTested.AspNetCore.Mvc.Caching` - contains setup and assertion methods for `IMemoryCache`
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
3+
using Xunit;
44

5-
// General Information about an assembly is controlled through the following
6-
// set of attributes. Change these attribute values to modify the information
7-
// associated with an assembly.
8-
[assembly: AssemblyConfiguration("")]
9-
[assembly: AssemblyCompany("")]
105
[assembly: AssemblyProduct("MyTested.AspNetCore.Mvc.ModelState.Test")]
11-
[assembly: AssemblyTrademark("")]
12-
13-
// Setting ComVisible to false makes the types in this assembly not visible
14-
// to COM components. If you need to access a type in this assembly from
15-
// COM, set the ComVisible attribute to true on that type.
166
[assembly: ComVisible(false)]
177

18-
// The following GUID is for the ID of the typelib if this project is exposed to COM
19-
[assembly: Guid("85915d02-3635-4bf3-9795-9fad378afe94")]
8+
[assembly: CollectionBehavior(DisableTestParallelization = true)]

0 commit comments

Comments
 (0)