Skip to content

Commit 23cb009

Browse files
committed
All library tests ported (#326)
1 parent c1afc25 commit 23cb009

File tree

18 files changed

+113
-54
lines changed

18 files changed

+113
-54
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Routing/ModelBindingActionInvoker.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Internal.Routing
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.Reflection;
@@ -14,6 +15,8 @@
1415

1516
public class ModelBindingActionInvoker : IModelBindingActionInvoker
1617
{
18+
private static readonly Type RouteActionResultMockType = typeof(RouteActionResultMock);
19+
1720
private readonly ControllerContext controllerContext;
1821
private readonly dynamic cacheEntry;
1922
private readonly dynamic cacheEntryMock;
@@ -49,6 +52,17 @@ public ModelBindingActionInvoker(
4952

5053
public async Task InvokeAsync()
5154
{
55+
var exposedInvoker = this.invoker.Exposed();
56+
57+
// Invoke the filter pipeline and execute the fake action mock.
58+
await exposedInvoker.InvokeAsync();
59+
60+
if (exposedInvoker._result.GetType() != RouteActionResultMockType)
61+
{
62+
// Filters short-circuited the action pipeline. Do not perform model binding.
63+
return;
64+
}
65+
5266
// Not initialized in the constructor because filters may
5367
// short-circuit the request and tests will fail with wrong exception message.
5468
this.arguments = new Dictionary<string, object>();
@@ -57,12 +71,10 @@ public async Task InvokeAsync()
5771
if (actionDescriptor.BoundProperties.Count == 0 &&
5872
actionDescriptor.Parameters.Count == 0)
5973
{
74+
// No parameters on the real action. Do not perform model binding.
6075
return;
6176
}
6277

63-
// Invokes the filter pipeline and executes a mocked action.
64-
await this.invoker.Exposed().InvokeAsync();
65-
6678
// Invokes the model binding on the real action.
6779
var controllerInstance = this.cacheEntry.ControllerFactory.Invoke(this.controllerContext);
6880

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Routing/MvcRouteResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static ResolvedRouteContext Resolve(IServiceProvider services, IRouter ro
4545
.Routers
4646
.Where(r => r.GetType() == WebFramework.Internals.MvcAttributeRouteHandler)
4747
.FirstOrDefault()
48-
.AsDynamic()
49-
?.Actions
48+
?.Exposed()
49+
.Actions
5050
?? actionSelector.SelectCandidates(routeContext);
5151

5252
actionDescriptor = actionSelector.SelectBestCandidate(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Routing
2+
{
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
internal class RouteActionResultMock : IActionResult
7+
{
8+
public Task ExecuteResultAsync(ActionContext context)
9+
=> Task.CompletedTask;
10+
}
11+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Routing/RouteControllerMock.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
{
33
internal class RouteControllerMock
44
{
5-
public void ActionMock()
6-
{
7-
// Intentionally left empty. Used by the route testing services to fake the actual action call.
8-
}
5+
// Used by the route testing services to fake the actual action call.
6+
public RouteActionResultMock ActionMock()
7+
=> new RouteActionResultMock();
98
}
109
}

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Routing/RouteDataResolver.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
public static class RouteDataResolver
99
{
10-
public static RouteData ResolveRouteData(IRouter router, HttpContext httpContext)
11-
{
12-
return ResolveRouteData(router, new RouteContext(httpContext));
13-
}
10+
public static RouteData ResolveRouteData(IRouter router, HttpContext httpContext)
11+
=> ResolveRouteData(router, new RouteContext(httpContext));
1412

1513
public static RouteData ResolveRouteData(IRouter router, RouteContext routeContext)
1614
{

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

Lines changed: 2 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;
34
using System.Linq.Expressions;
45
using Application;
56
using Http;
@@ -42,7 +43,7 @@ public virtual RouteData RouteData
4243
if (this.routeData == null)
4344
{
4445
this.routeData = this.HttpContext.GetRouteData();
45-
if (this.routeData == null)
46+
if (this.routeData == null || !this.routeData.Routers.Any())
4647
{
4748
this.routeData = RouteDataResolver.ResolveRouteData(TestApplication.Router, this.HttpContext);
4849
}

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExposedObject.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ public ExposedObject(object instance)
1919
this.type = instance?.GetType();
2020
}
2121

22-
public ExposedObject(Type type)
23-
{
24-
this.type = type;
25-
}
22+
public ExposedObject(Type type) => this.type = type;
2623

2724
public object Object => this.instance ?? this.type;
2825

@@ -47,7 +44,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
4744
return true;
4845
}
4946

50-
var field = this.type.GetField(binder.Name);
47+
var field = this.type.GetField(binder.Name, Flags);
5148

5249
if (field != null)
5350
{
@@ -70,7 +67,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
7067
return true;
7168
}
7269

73-
var field = this.type.GetField(binder.Name);
70+
var field = this.type.GetField(binder.Name, Flags);
7471

7572
if (field != null)
7673
{

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Controllers/ControllerBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ protected override TController TryCreateComponentWithFactory()
6666
protected override void ActivateComponent()
6767
=> this.Services
6868
.GetServices(WebFramework.Internals.ControllerPropertyActivator)
69-
?.ForEach(a => a.Exposed().Activate(this.TestContext.ComponentContext, this.TestContext.Component));
69+
?.ForEach(a => a
70+
.Exposed()
71+
.Activate(this.TestContext.ComponentContext, this.TestContext.Component));
7072
}
7173
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc
22
{
3+
using System.Linq;
34
using Builders.Base;
45
using Builders.Contracts.Base;
56
using Internal.Application;
@@ -52,6 +53,10 @@ public static TBuilder WithRouteData<TBuilder>(
5253
if (testContext.RouteData == null)
5354
{
5455
testContext.RouteData = new RouteData();
56+
}
57+
58+
if (!testContext.RouteData.Routers.Any())
59+
{
5560
testContext.RouteData.Routers.Add(TestApplication.Router);
5661
}
5762

src/MyTested.AspNetCore.Mvc.ViewFeatures/ModelErrorTestBuilderViewFeaturesExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static IModelErrorDetailsTestBuilder<TModel> ContainingErrorFor<TModel, T
3333
{
3434
var actualModelErrorTestBuilder = (ModelErrorTestBuilder<TModel>)modelErrorTestBuilder;
3535

36-
var memberName = ExpressionHelper.GetExpressionText(memberWithError, ExpressionTextCache);
36+
string memberName = ExpressionHelper.GetExpressionText(memberWithError, ExpressionTextCache);
3737
actualModelErrorTestBuilder.ContainingError(memberName);
3838

3939
return new ModelErrorDetailsTestBuilder<TModel>(
@@ -57,7 +57,7 @@ public static IAndModelErrorTestBuilder<TModel> ContainingNoErrorFor<TModel, TMe
5757
{
5858
var actualModelErrorTestBuilder = (ModelErrorTestBuilder<TModel>)modelErrorTestBuilder;
5959

60-
var memberName = ExpressionHelper.GetExpressionText(memberWithNoError, ExpressionTextCache);
60+
string memberName = ExpressionHelper.GetExpressionText(memberWithNoError, ExpressionTextCache);
6161
if (actualModelErrorTestBuilder.ModelState.ContainsKey(memberName))
6262
{
6363
actualModelErrorTestBuilder.ThrowNewModelErrorAssertionException(

0 commit comments

Comments
 (0)