Skip to content

Commit 6d2f6e4

Browse files
committed
Fixed bug in pipeline testing where the HttpContext setup was not passed from the route to the controller (#282)
1 parent 6d3d6d5 commit 6d2f6e4

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,28 @@
1111

1212
public abstract class HttpTestContext
1313
{
14-
private HttpContextMock httpContextMock;
1514
private RouteData routeData;
1615

1716
protected HttpTestContext()
1817
{
1918
TestHelper.ExecuteTestCleanup();
20-
this.httpContextMock = TestHelper.CreateHttpContextMock();
19+
this.HttpContextMock = TestHelper.CreateHttpContextMock();
2120
}
2221

2322
public HttpContext HttpContext
2423
{
25-
get => this.httpContextMock;
24+
get => this.HttpContextMock;
2625

2726
set
2827
{
2928
CommonValidator.CheckForNullReference(value, nameof(this.HttpContext));
30-
this.httpContextMock = HttpContextMock.From(value);
31-
TestHelper.SetHttpContextToAccessor(this.httpContextMock);
29+
this.HttpContextMock = HttpContextMock.From(value);
30+
TestHelper.SetHttpContextToAccessor(this.HttpContextMock);
3231
}
3332
}
3433

34+
public HttpContextMock HttpContextMock { get; private set; }
35+
3536
public HttpRequest HttpRequest => this.HttpContext.Request;
3637

3738
public HttpResponse HttpResponse => this.HttpContext.Response;
@@ -64,7 +65,5 @@ public virtual RouteData RouteData
6465
public ISession Session => this.HttpContext.Session;
6566

6667
public abstract string ExceptionMessagePrefix { get; }
67-
68-
public HttpContextMock HttpContextMock => this.httpContextMock;
6968
}
7069
}

src/MyTested.AspNetCore.Mvc.Pipeline/ControllerRouteTestBuilderPipelineExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ public static IActionResultTestBuilder<MethodResult> Which<TController>(
3939
{
4040
var actualBuilder = (ControllerRouteTestBuilder<TController>)builder;
4141

42+
var routeContext = actualBuilder.RouteContext;
4243
var actionCall = actualBuilder.ActionCallExpression;
4344

4445
var whichControllerInstanceBuilder = new WhichControllerInstanceBuilder<TController>(new ControllerTestContext
4546
{
47+
HttpContext = routeContext.HttpContext,
48+
RouteData = routeContext.RouteData,
4649
ComponentConstructionDelegate = () => null,
4750
MethodCall = actionCall
4851
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected ShouldMapTestBuilder(
4343
: this(testContext)
4444
=> this.actionCallExpression = actionCallExpression;
4545

46-
private RouteContext RouteContext => this.TestContext.RouteContext;
46+
public RouteContext RouteContext => this.TestContext.RouteContext;
4747

4848
/// <inheritdoc />
4949
public IAndResolvedRouteTestBuilder ToAction(string actionName)

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ public class RouteTestContext : HttpTestContext
88
{
99
private RouteContext routeContext;
1010

11-
public RouteTestContext()
12-
{
13-
this.SetAuthentication();
14-
}
11+
public RouteTestContext() => this.SetAuthentication();
1512

1613
public IRouter Router { get; internal set; }
1714

0 commit comments

Comments
 (0)