1

I have a route that is defined like this:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = "" }, // Parameter defaults
            new { home = new HomePageConstraint() }
        );

public class HomePageConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return !httpContext.Request.RawUrl.StartsWith("/home", StringComparison.InvariantCultureIgnoreCase);
    }
}

And I am trying to test it like this:

[Test]
public void Home_Load_Homepage()
{
    "~/".ShouldMapTo<HomeController>(x => x.Index());
}

The problem I have is that the httpContext is null, so the test fails. how can I inject http context into a constraint?

1 Answer 1

1

In the end I did this:

var context = new FakeHttpContext("~/");
var fakeRequest = new FakeRequest("~/", new Uri("http://localhost/"), new Uri("http://localhost/"));
context.SetRequest(fakeRequest);

var route = RouteTable.Routes.GetRouteData(context);

route.ShouldMapTo<HomeController>(x => x.Index());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.