Unit test constraints without HttpContext (#755)

This commit is contained in:
James Newton-King 2018-08-29 17:34:54 +12:00 committed by GitHub
parent 99c4f2f36a
commit 4e9e33a223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 23 deletions

View File

@ -10,21 +10,12 @@ namespace Microsoft.AspNetCore.Routing.Tests
{
public class ConstraintsTestHelper
{
public static bool TestConstraint(IRouteConstraint constraint, object value, Action<IRouter> routeConfig = null)
public static bool TestConstraint(IRouteConstraint constraint, object value)
{
var context = new Mock<HttpContext>();
var route = new RouteCollection();
if (routeConfig != null)
{
routeConfig(route);
}
var parameterName = "fake";
var values = new RouteValueDictionary() { { parameterName, value } };
var routeDirection = RouteDirection.IncomingRequest;
return constraint.Match(context.Object, route, parameterName, values, routeDirection);
return constraint.Match(httpContext: null, route: null, parameterName, values, routeDirection);
}
}
}

View File

@ -691,8 +691,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.Equal(expectedValues.OrderBy(kvp => kvp.Key), constraint.Values.OrderBy(kvp => kvp.Key));
}
[Fact]
public void GetLink_InlineConstraints_Success()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_Success(bool hasHttpContext)
{
// Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -700,7 +702,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" },
constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(new { });
var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act
var link = linkGenerator.GetLink(
@ -732,8 +734,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.False(canGenerateLink);
}
[Fact]
public void GetLink_InlineConstraints_OptionalParameter_ValuePresent()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_OptionalParameter_ValuePresent(bool hasHttpContext)
{
// Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -741,7 +745,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" },
constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(ambientValues: new { });
var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act
var link = linkGenerator.GetLink(httpContext, new { action = "Index", controller = "Home", id = 98 });
@ -789,8 +793,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.False(canGenerateLink);
}
[Fact]
public void GetLink_InlineConstraints_MultipleInlineConstraints()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_MultipleInlineConstraints(bool hasHttpContext)
{
// Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -798,7 +804,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" },
constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(ambientValues: new { });
var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act
var link = linkGenerator.GetLink(
@ -809,8 +815,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.Equal("/Home/Index/14", link);
}
[Fact]
public void GetLink_InlineConstraints_CompositeInlineConstraint_Fails()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_CompositeInlineConstraint_Fails(bool hasHttpContext)
{
// Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -818,7 +826,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" },
constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(ambientValues: new { });
var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act
var canGenerateLink = linkGenerator.TryGetLink(