Skip to content

Commit 7776934

Browse files
committed
Created all pipeline interfaces allowing assertions from a route to a controller (#282)
1 parent 08711e2 commit 7776934

File tree

9 files changed

+118
-12
lines changed

9 files changed

+118
-12
lines changed

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Contracts/Actions/IActionResultTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// Used for testing the action and its result.
88
/// </summary>
99
/// <typeparam name="TActionResult">Type of action result to be tested.</typeparam>
10-
public interface IActionResultTestBuilder<TActionResult> : IBaseTestBuilderWithActionResult
10+
public interface IActionResultTestBuilder<TActionResult> : IBaseTestBuilderWithInvokedAction
1111
{
1212
/// <summary>
1313
/// Used for testing the action's additional data - action attributes, HTTP response, view bag and more.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Pipeline
2+
{
3+
/// <summary>
4+
/// Used for adding AndAlso() method to the controller instance builder from a route test.
5+
/// </summary>
6+
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
7+
public interface IAndWhichControllerInstanceBuilder<TController> : IWhichControllerInstanceBuilder<TController>
8+
where TController : class
9+
{
10+
/// <summary>
11+
/// AndAlso method for better readability when building controller instance from a route test.
12+
/// </summary>
13+
/// <returns>The same <see cref="IWhichControllerInstanceBuilder{TController}"/>.</returns>
14+
IWhichControllerInstanceBuilder<TController> AndAlso();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Pipeline
2+
{
3+
using Builders.Contracts.Actions;
4+
using Controllers;
5+
6+
/// <summary>
7+
/// Used for building the controller which will be tested after a route assertion.
8+
/// </summary>
9+
/// <typeparam name="TController"></typeparam>
10+
public interface IWhichControllerInstanceBuilder<TController>
11+
: IBaseControllerBuilder<TController, IAndWhichControllerInstanceBuilder<TController>>,
12+
IActionResultTestBuilder<object>
13+
where TController : class
14+
{
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using System;
4+
using Builders.Contracts.Actions;
5+
using Builders.Contracts.Controllers;
6+
using Builders.Contracts.Pipeline;
7+
using Builders.Contracts.Routing;
8+
9+
/// <summary>
10+
/// Contains extension methods for <see cref="IControllerRouteTestBuilder{TController}"/>.
11+
/// </summary>
12+
public static class ControllerRouteTestBuilderPipelineExtensions
13+
{
14+
/// <summary>
15+
/// Allows the route test to continue the assertion chain on the matched controller action.
16+
/// </summary>
17+
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
18+
/// <param name="builder">Instance of <see cref="IControllerRouteTestBuilder{TController}"/> type.</param>
19+
/// <returns>Test builder of <see cref="IWhichControllerInstanceBuilder{TController}"/> type.</returns>
20+
public static IWhichControllerInstanceBuilder<TController> Which<TController>(
21+
this IControllerRouteTestBuilder<TController> builder)
22+
where TController : class
23+
{
24+
return null;
25+
}
26+
27+
/// <summary>
28+
/// Allows the route test to continue the assertion chain on the matched controller action.
29+
/// </summary>
30+
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
31+
/// <param name="builder">Instance of <see cref="IControllerRouteTestBuilder{TController}"/> type.</param>
32+
/// <param name="controllerInstanceBuilder">Builder for creating the controller instance.</param>
33+
/// <returns>Test builder of <see cref="IActionResultTestBuilder{TActionResult}"/> type.</returns>
34+
public static IActionResultTestBuilder<object> Which<TController>(
35+
this IControllerRouteTestBuilder<TController> builder,
36+
Action<IControllerInstanceBuilder<TController>> controllerInstanceBuilder)
37+
where TController : class
38+
{
39+
return null;
40+
}
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Routing
2+
{
3+
/// <summary>
4+
/// Used for testing controller route details.
5+
/// </summary>
6+
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
7+
public interface IControllerRouteTestBuilder<TController> : IAndResolvedRouteTestBuilder
8+
{
9+
}
10+
}

src/MyTested.AspNetCore.Mvc.Routing/Builders/Contracts/Routing/IShouldMapTestBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ IAndResolvedRouteTestBuilder To<TController>()
4444
/// </summary>
4545
/// <typeparam name="TController">Type of expected resolved controller.</typeparam>
4646
/// <param name="actionCall">Method call expression indicating the expected resolved action.</param>
47-
/// <returns>The <see cref="IAndResolvedRouteTestBuilder"/>.</returns>
48-
IAndResolvedRouteTestBuilder To<TController>(Expression<Action<TController>> actionCall)
47+
/// <returns>The <see cref="IControllerRouteTestBuilder{TController}"/>.</returns>
48+
IControllerRouteTestBuilder<TController> To<TController>(Expression<Action<TController>> actionCall)
4949
where TController : class;
5050

5151
/// <summary>
5252
/// Tests whether the built route is resolved to the asynchronous action provided by the expression.
5353
/// </summary>
5454
/// <typeparam name="TController">Type of expected resolved controller.</typeparam>
5555
/// <param name="actionCall">Method call expression indicating the expected resolved asynchronous action.</param>
56-
/// <returns>The <see cref="IAndResolvedRouteTestBuilder"/>.</returns>
57-
IAndResolvedRouteTestBuilder To<TController>(Expression<Func<TController, Task>> actionCall)
56+
/// <returns>The <see cref="IControllerRouteTestBuilder{TController}"/>.</returns>
57+
IControllerRouteTestBuilder<TController> To<TController>(Expression<Func<TController, Task>> actionCall)
5858
where TController : class;
5959

6060
/// <summary>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Routing
2+
{
3+
using Contracts.Routing;
4+
using Internal.TestContexts;
5+
6+
/// <summary>
7+
/// Used for testing controller route details.
8+
/// </summary>
9+
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
10+
public class ControllerRouteTestBuilder<TController>
11+
: ShouldMapTestBuilder,
12+
IControllerRouteTestBuilder<TController>
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="ControllerRouteTestBuilder{TController}"/> class.
16+
/// </summary>
17+
/// <param name="testContext"><see cref="RouteTestContext"/> containing data about the currently executed assertion chain.</param>
18+
public ControllerRouteTestBuilder(RouteTestContext testContext)
19+
: base(testContext)
20+
{
21+
}
22+
}
23+
}

src/MyTested.AspNetCore.Mvc.Routing/Builders/Routing/ShouldMapTestBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@ public IAndResolvedRouteTestBuilder To<TController>()
227227
}
228228

229229
/// <inheritdoc />
230-
public IAndResolvedRouteTestBuilder To<TController>(Expression<Action<TController>> actionCall)
230+
public IControllerRouteTestBuilder<TController> To<TController>(Expression<Action<TController>> actionCall)
231231
where TController : class
232-
=> this.ProcessRouteLambdaExpression(actionCall);
232+
=> this.ProcessRouteLambdaExpression<TController>(actionCall);
233233

234234
/// <inheritdoc />
235-
public IAndResolvedRouteTestBuilder To<TController>(Expression<Func<TController, Task>> actionCall)
235+
public IControllerRouteTestBuilder<TController> To<TController>(Expression<Func<TController, Task>> actionCall)
236236
where TController : class
237-
=> this.ProcessRouteLambdaExpression(actionCall);
237+
=> this.ProcessRouteLambdaExpression<TController>(actionCall);
238238

239239
/// <inheritdoc />
240240
public void ToNonExistingRoute()
@@ -251,11 +251,11 @@ public void ToNonExistingRoute()
251251
/// <inheritdoc />
252252
public IResolvedRouteTestBuilder AndAlso() => this;
253253

254-
private IAndResolvedRouteTestBuilder ProcessRouteLambdaExpression(LambdaExpression actionCall)
254+
private IControllerRouteTestBuilder<TController> ProcessRouteLambdaExpression<TController>(LambdaExpression actionCall)
255255
{
256256
this.actionCallExpression = actionCall;
257257
this.ValidateRouteInformation();
258-
return this;
258+
return new ControllerRouteTestBuilder<TController>(this.TestContext);
259259
}
260260

261261
private void ValidateRouteInformation()

src/MyTested.AspNetCore.Mvc.Routing/Internal/TestContexts/RouteTestContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using Microsoft.AspNetCore.Routing;
55
using Microsoft.AspNetCore.Http.Features.Authentication;
6-
using Routing;
76

87
public class RouteTestContext : HttpTestContext
98
{

0 commit comments

Comments
 (0)