Skip to content

Commit 0dc9014

Browse files
committed
Abstracted ActionTestContext for all action related test contexts (#66)
1 parent 30b5f59 commit 0dc9014

8 files changed

Lines changed: 136 additions & 123 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.TestContexts
2+
{
3+
using System.Linq;
4+
using Application;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Routing;
7+
using Routing;
8+
using Utilities.Validators;
9+
10+
public abstract class ActionTestContext<TComponentContext> : ComponentTestContext
11+
where TComponentContext : ActionContext
12+
{
13+
private TComponentContext componentContext;
14+
private RouteData expressionRouteData;
15+
16+
public override RouteData RouteData
17+
{
18+
get
19+
{
20+
var routeData = base.RouteData;
21+
if (routeData != null)
22+
{
23+
return routeData;
24+
}
25+
26+
if (this.expressionRouteData == null && this.MethodCall != null)
27+
{
28+
this.expressionRouteData = RouteExpressionParser.ResolveRouteData(TestApplication.Router, this.MethodCall);
29+
}
30+
31+
return this.expressionRouteData;
32+
}
33+
34+
set
35+
{
36+
CommonValidator.CheckForNullReference(value, nameof(RouteData));
37+
this.ComponentContext.RouteData = value;
38+
base.RouteData = value;
39+
}
40+
}
41+
42+
public TComponentContext ComponentContext
43+
{
44+
get
45+
{
46+
if (this.componentContext == null)
47+
{
48+
this.componentContext = this.DefaultComponentContext;
49+
if (!this.componentContext.RouteData.Values.Any())
50+
{
51+
this.componentContext.RouteData = this.RouteData ?? this.componentContext.RouteData;
52+
}
53+
}
54+
55+
return this.componentContext;
56+
}
57+
58+
set
59+
{
60+
CommonValidator.CheckForNullReference(value, nameof(TComponentContext));
61+
this.componentContext = value;
62+
}
63+
}
64+
65+
protected abstract TComponentContext DefaultComponentContext { get; }
66+
}
67+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.TestContexts
2+
{
3+
using System;
4+
using System.Linq.Expressions;
5+
6+
/// <summary>
7+
/// Contains information about invoked method.
8+
/// </summary>
9+
/// <typeparam name="TResult">The method return type.</typeparam>
10+
public class InvocationTestContext<TResult>
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="InvocationTestContext{TActionResult}"/> class.
14+
/// </summary>
15+
/// <param name="methodName">Name of the method.</param>
16+
/// <param name="methodCall"><see cref="LambdaExpression"/> representing the method call.</param>
17+
/// <param name="methodResult">Method return value.</param>
18+
/// <param name="caughtException">Caught exception during method execution.</param>
19+
public InvocationTestContext(string methodName, LambdaExpression methodCall, TResult methodResult, Exception caughtException)
20+
{
21+
this.MethodName = methodName;
22+
this.MethodCall = methodCall;
23+
this.MethodResult = methodResult;
24+
this.CaughtException = caughtException;
25+
}
26+
27+
/// <summary>
28+
/// Gets the name of the method.
29+
/// </summary>
30+
/// <value>The method name as string.</value>
31+
public string MethodName { get; set; }
32+
33+
public LambdaExpression MethodCall { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the return value of the method.
37+
/// </summary>
38+
/// <value>The method result as TResult.</value>
39+
public TResult MethodResult { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets the caught exception during the method execution.
43+
/// </summary>
44+
/// <value>Method execution exception.</value>
45+
public Exception CaughtException { get; set; }
46+
}
47+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public IActionResultTestBuilder<TActionResult> Calling<TActionResult>(Expression
3838

3939
try
4040
{
41-
actionResult = AsyncHelper.RunSync(() => actionInfo.ActionResult);
41+
actionResult = AsyncHelper.RunSync(() => actionInfo.MethodResult);
4242
}
4343
catch (Exception exception)
4444
{
@@ -81,7 +81,7 @@ public IVoidActionResultTestBuilder Calling(Expression<Func<TController, Task>>
8181

8282
try
8383
{
84-
AsyncHelper.RunSync(() => actionInfo.ActionResult);
84+
AsyncHelper.RunSync(() => actionInfo.MethodResult);
8585
}
8686
catch (Exception exception)
8787
{
@@ -94,7 +94,7 @@ public IVoidActionResultTestBuilder Calling(Expression<Func<TController, Task>>
9494
return new VoidActionResultTestBuilder(this.TestContext);
9595
}
9696

97-
private ActionTestContext<TActionResult> GetAndValidateActionResult<TActionResult>(Expression<Func<TController, TActionResult>> actionCall)
97+
private InvocationTestContext<TActionResult> GetAndValidateActionResult<TActionResult>(Expression<Func<TController, TActionResult>> actionCall)
9898
{
9999
var actionName = this.GetAndValidateAction(actionCall);
100100
var actionResult = default(TActionResult);
@@ -109,7 +109,7 @@ private ActionTestContext<TActionResult> GetAndValidateActionResult<TActionResul
109109
caughtException = exception;
110110
}
111111

112-
return new ActionTestContext<TActionResult>(actionName, actionCall, actionResult, caughtException);
112+
return new InvocationTestContext<TActionResult>(actionName, actionCall, actionResult, caughtException);
113113
}
114114

115115
private string GetAndValidateAction(LambdaExpression actionCall)
@@ -141,15 +141,15 @@ private void ValidateModelState(LambdaExpression actionCall)
141141
{
142142
if (argument.Value != null)
143143
{
144-
validator.Validate(this.TestContext.ControllerContext, argument.Value);
144+
validator.Validate(this.TestContext.ComponentContext, argument.Value);
145145
}
146146
});
147147
}
148148
}
149149

150150
private void SetActionDescriptor(MethodInfo methodInfo)
151151
{
152-
var controllerContext = this.TestContext.ControllerContext;
152+
var controllerContext = this.TestContext.ComponentContext;
153153
if (controllerContext.ActionDescriptor?.MethodInfo == null)
154154
{
155155
var controllerActionDescriptorCache = this.Services.GetService<IControllerActionDescriptorCache>();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public IAndControllerBuilder<TController> WithControllerContext(Action<Controlle
3131
public IAndControllerBuilder<TController> WithActionContext(ActionContext actionContext)
3232
{
3333
CommonValidator.CheckForNullReference(actionContext, nameof(ActionContext));
34-
this.TestContext.ControllerContext = ControllerContextMock.FromActionContext(this.TestContext, actionContext);
34+
this.TestContext.ComponentContext = ControllerContextMock.FromActionContext(this.TestContext, actionContext);
3535
return this;
3636
}
3737

@@ -51,15 +51,15 @@ public IAndControllerBuilder<TController> WithSetup(Action<TController> controll
5151

5252
protected override void PrepareComponentContext()
5353
{
54-
var controllerContext = this.TestContext.ControllerContext;
54+
var controllerContext = this.TestContext.ComponentContext;
5555
this.controllerContextAction?.Invoke(controllerContext);
5656
}
5757

5858
protected override void PrepareComponent()
5959
{
60-
var controllerPropertyActivators = this.Services.GetServices<IControllerPropertyActivator>();
61-
62-
controllerPropertyActivators.ForEach(a => a.Activate(this.TestContext.ControllerContext, this.TestContext.Component));
60+
this.Services
61+
.GetServices<IControllerPropertyActivator>()
62+
?.ForEach(a => a.Activate(this.TestContext.ComponentContext, this.TestContext.Component));
6363

6464
this.TestContext.ComponentPreparationDelegate?.Invoke();
6565

src/MyTested.AspNetCore.Mvc.Controllers/Internal/TestContexts/ActionTestContext.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,25 @@
11
namespace MyTested.AspNetCore.Mvc.Internal.TestContexts
22
{
3-
using System.Linq;
4-
using Application;
53
using Controllers;
64
using Microsoft.AspNetCore.Mvc;
75
using Microsoft.AspNetCore.Mvc.ModelBinding;
8-
using Microsoft.AspNetCore.Routing;
96
using Utilities.Extensions;
10-
using Utilities.Validators;
11-
using Routing;
127

13-
public class ControllerTestContext : ComponentTestContext
8+
public class ControllerTestContext : ActionTestContext<ControllerContext>
149
{
15-
private ControllerContext controllerContext;
16-
private RouteData expressionRouteData;
17-
18-
public ModelStateDictionary ModelState => this.ControllerContext.ModelState;
10+
public ModelStateDictionary ModelState => this.ComponentContext.ModelState;
1911

2012
public override string ExceptionMessagePrefix => $"When calling {this.MethodName} action in {this.Component.GetName()} expected";
21-
22-
public override RouteData RouteData
23-
{
24-
get
25-
{
26-
var routeData = base.RouteData;
27-
if (routeData != null)
28-
{
29-
return routeData;
30-
}
31-
32-
if (this.expressionRouteData == null && this.MethodCall != null)
33-
{
34-
this.expressionRouteData = RouteExpressionParser.ResolveRouteData(TestApplication.Router, this.MethodCall);
35-
}
3613

37-
return this.expressionRouteData;
38-
}
39-
40-
set
41-
{
42-
CommonValidator.CheckForNullReference(value, nameof(RouteData));
43-
this.ControllerContext.RouteData = value;
44-
base.RouteData = value;
45-
}
46-
}
14+
protected override ControllerContext DefaultComponentContext
15+
=> ControllerContextMock.Default(this);
4716

48-
internal ControllerContext ControllerContext
49-
{
50-
get
51-
{
52-
if (this.controllerContext == null)
53-
{
54-
this.controllerContext = ControllerContextMock.Default(this);
55-
if (!this.controllerContext.RouteData.Values.Any())
56-
{
57-
this.controllerContext.RouteData = this.RouteData ?? this.controllerContext.RouteData;
58-
}
59-
}
60-
61-
return this.controllerContext;
62-
}
63-
64-
set
65-
{
66-
CommonValidator.CheckForNullReference(value, nameof(ControllerContext));
67-
this.controllerContext = value;
68-
}
69-
}
70-
71-
internal void Apply<TActionResult>(ActionTestContext<TActionResult> actionTestContext)
17+
internal void Apply<TMethodResult>(InvocationTestContext<TMethodResult> invocationTestContext)
7218
{
73-
this.MethodName = actionTestContext.ActionName;
74-
this.MethodCall = actionTestContext.ActionCall;
75-
this.MethodResult = actionTestContext.ActionResult;
76-
this.CaughtException = actionTestContext.CaughtException;
19+
this.MethodName = invocationTestContext.MethodName;
20+
this.MethodCall = invocationTestContext.MethodCall;
21+
this.MethodResult = invocationTestContext.MethodResult;
22+
this.CaughtException = invocationTestContext.CaughtException;
7723
}
7824
}
7925
}

src/MyTested.AspNetCore.Mvc.Controllers/Utilities/Extensions/UrlHelperExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static string GenerateLink(
6363

6464
linkGenerationTestContext.Controller = linkGenerationTestContext.Controller
6565
?? controllerTestContext.RouteData.Values["controller"] as string
66-
?? controllerTestContext.ControllerContext.ActionDescriptor.ControllerName;
66+
?? controllerTestContext.ComponentContext.ActionDescriptor.ControllerName;
6767

6868
uri = urlHelper.Action(
6969
linkGenerationTestContext.Action,

src/MyTested.AspNetCore.Mvc.Controllers/Utilities/Validators/RouteActionResultValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public static void ValidateExpressionLink(
269269
LambdaExpression expectedRouteValuesAsLambdaExpression,
270270
Action<string, string, string> failedValidationAction)
271271
{
272-
var actionContext = controllerTestContext.ControllerContext;
272+
var actionContext = controllerTestContext.ComponentContext;
273273
if (!actionContext.RouteData.Routers.Any())
274274
{
275275
actionContext.RouteData.Routers.Add(TestApplication.Router);

0 commit comments

Comments
 (0)