Skip to content

Commit 28a00dc

Browse files
committed
WithRouteData moved to MyTested.AspNetCore.Mvc.Routing (#125)
1 parent e883b09 commit 28a00dc

File tree

9 files changed

+63
-62
lines changed

9 files changed

+63
-62
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilderWithComponent.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Base
22
{
3+
using System;
34
using Contracts.Base;
45
using Internal.TestContexts;
56
using Utilities.Validators;
@@ -38,5 +39,9 @@ private set
3839
this.testContext = value;
3940
}
4041
}
42+
43+
public Action<ComponentTestContext> ComponentPreparationAction { get; set; }
44+
45+
public Action<ComponentTestContext> PreInvocationAction { get; set; }
4146
}
4247
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public IEnumerable<object> MethodAttributes
9999
return this.methodAttributes;
100100
}
101101
}
102+
102103
public Exception CaughtException { get; set; }
103104

104105
public object Model

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/Controllers/IControllerBuilder.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,6 @@ public interface IControllerBuilder<TController> : IBaseTestBuilderWithComponent
5151
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
5252
IAndControllerBuilder<TController> WithActionContext(Action<ActionContext> actionContextSetup);
5353

54-
/// <summary>
55-
/// Indicates that route values should be extracted from the provided action call expression.
56-
/// </summary>
57-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
58-
IAndControllerBuilder<TController> WithRouteData();
59-
60-
/// <summary>
61-
/// Indicates that route values should be extracted from the provided action call expression adding the given additional values.
62-
/// </summary>
63-
/// <param name="additionalRouteValues">Anonymous object containing route values.</param>
64-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
65-
IAndControllerBuilder<TController> WithRouteData(object additionalRouteValues);
66-
6754
/// <summary>
6855
/// Configures a service with scoped lifetime by using the provided <see cref="Action"/> delegate.
6956
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private ActionTestContext<TActionResult> GetAndValidateActionResult<TActionResul
123123
private string GetAndValidateAction(LambdaExpression actionCall)
124124
{
125125
this.BuildControllerIfNotExists();
126-
this.SetRouteData(actionCall);
126+
this.PreInvocationAction?.Invoke(this.TestContext);
127127

128128
var methodInfo = ExpressionParser.GetMethodInfo(actionCall);
129129

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public partial class ControllerBuilder<TController> : BaseTestBuilderWithCompone
3030
private Action<TController> controllerSetupAction;
3131
private bool isPreparedForTesting;
3232
private bool enabledValidation;
33-
private bool resolveRouteValues;
34-
private object additionalRouteValues;
3533

3634
/// <summary>
3735
/// Initializes a new instance of the <see cref="ControllerBuilder{TController}"/> class.
@@ -71,8 +69,6 @@ private TController Controller
7169
}
7270
}
7371

74-
public Action<ControllerTestContext> ControllerPreparationAction { get; set; }
75-
7672
private new MockedHttpContext HttpContext => this.TestContext.MockedHttpContext;
7773

7874
private HttpRequest HttpRequest => this.HttpContext.Request;

src/MyTested.AspNetCore.Mvc.Core/Builders/Controllers/ControllerHttpBuilder.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void PrepareController()
117117

118118
controllerPropertyActivators.ForEach(a => a.Activate(this.TestContext.ControllerContext, this.TestContext.Component));
119119

120-
this.ControllerPreparationAction?.Invoke(this.TestContext);
120+
this.ComponentPreparationAction?.Invoke(this.TestContext);
121121

122122
this.controllerSetupAction?.Invoke(this.TestContext.ComponentAs<TController>());
123123
}

src/MyTested.AspNetCore.Mvc.Core/Internal/TestContexts/ControllerTestContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
using Microsoft.AspNetCore.Mvc;
77
using Microsoft.AspNetCore.Mvc.ModelBinding;
88
using Microsoft.AspNetCore.Routing;
9-
using Routing;
109
using Utilities.Extensions;
1110
using Utilities.Validators;
11+
using Routing;
1212

1313
public class ControllerTestContext : ComponentTestContext
1414
{
1515
private ControllerContext controllerContext;
1616
private RouteData expressionRouteData;
17+
18+
public ModelStateDictionary ModelState => this.ControllerContext.ModelState;
19+
20+
public override string ExceptionMessagePrefix => $"When calling {this.MethodName} action in {this.Component.GetName()} expected";
1721

1822
public override RouteData RouteData
1923
{
@@ -41,10 +45,6 @@ public override RouteData RouteData
4145
}
4246
}
4347

44-
public ModelStateDictionary ModelState => this.ControllerContext.ModelState;
45-
46-
public override string ExceptionMessagePrefix => $"When calling {this.MethodName} action in {this.Component.GetName()} expected";
47-
4848
internal ControllerContext ControllerContext
4949
{
5050
get
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using Builders.Base;
4+
using Builders.Contracts.Base;
5+
using Internal.Application;
6+
using Internal.Routing;
7+
using Utilities.Extensions;
8+
9+
/// <summary>
10+
/// Contains HTTP extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>.
11+
/// </summary>
12+
public static class ComponentBuilderRoutingExtensions
13+
{
14+
/// <summary>
15+
/// Indicates that route values should be extracted from the provided method call expression.
16+
/// </summary>
17+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
18+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
19+
/// <returns>The same component builder.</returns>
20+
public static TBuilder WithRouteData<TBuilder>(
21+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
22+
where TBuilder : IBaseTestBuilder
23+
{
24+
return builder.WithRouteData(null);
25+
}
26+
27+
/// <summary>
28+
/// Indicates that route values should be extracted from the provided action call expression adding the given additional values.
29+
/// </summary>
30+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
31+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
32+
/// <param name="additionalRouteValues">Anonymous object containing route values.</param>
33+
/// <returns>The same component builder.</returns>
34+
public static TBuilder WithRouteData<TBuilder>(
35+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
36+
object additionalRouteValues)
37+
where TBuilder : IBaseTestBuilder
38+
{
39+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
40+
41+
actualBuilder.PreInvocationAction += testContext =>
42+
{
43+
testContext.RouteData = RouteExpressionParser.ResolveRouteData(TestApplication.Router, testContext.MethodCall);
44+
testContext.RouteData.Values.Add(additionalRouteValues);
45+
};
46+
47+
return actualBuilder.Builder;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)