Additional setup in TestUtility.GetHttpContext(..)

Jan 12, 2012 at 10:40 AM


Thanks for the great test helpers!

I was curious of you could add additional setups in the TestUtility.GetHttpContext(..) or offer an

extension point where we can add additional Moq Setup calls to the HttpContextBase

var mockContext = new Mock<HttpContextBase>();


mockContext.Setup(context => context.Request.AppRelativeCurrentExecutionFilePath).Returns(filePath);

  mockContext.Setup(context => context.Items).Returns(new Dictionary<object, object>());

The reason for this is that we have some routing infrastructure which makes use of the HttpContext.Items property which blows up without these additional setups.


Jan 12, 2012 at 2:53 PM

Getting ready to leave town later today, but let me see what I can do for you. :)

Jan 12, 2012 at 4:55 PM

The first setup is already in there. I can add the Items setup pretty easily though. Give me an hour or so...

Jan 12, 2012 at 5:19 PM

All done. Update via Nuget to and let me know if this works for you.

Jan 13, 2012 at 1:10 PM

Great! Thank you!

Apr 22, 2012 at 8:53 PM

I'd also like to see an extension point for modifying the mock context. I need to add a few different host headers which are very specific to my app which of course shouldn't be part of the core code.

Would this be possible?

Apr 25, 2012 at 7:55 PM

That sounds pretty reasonable. I will look into it sometime this week.

Apr 26, 2012 at 3:54 AM

As I look at it, I think the best thing to do would be to simply expose the context in the RouteInfo and RequestInfo objects. That way you'll be able to alter it as required, or even get the mock of it and perform additional setup then reset it.

Apr 26, 2012 at 11:18 AM

Yeah, that works, as long as it's accessible

Apr 26, 2012 at 5:03 PM

Okay, try downloading the latest checkin.

Let me know if this works for what you had in mind.

There is an HttpContext property on both the RouteInfo and RequestInfo objects. So you can access it as follows:

         var tester = new RouteTester(routeCollection);
         RouteInfo routeInfo = tester.WithRouteInfo("Home""About");
         HttpContextBase context = routeInfo.HttpContext;
         // do whatever you need to the context here.
// you can also get the original mock using Mock.Get(context) and add setups


Apr 26, 2012 at 9:28 PM

Thats works great, I added an extension method so I can write

var tester = new RouteTester(routes);

tester.WithIncomingRequest("/").WithHostName("").ShouldMatchRoute("Home", "Index");

Any plans to update the nuget package any time soon?

Thanks for your great work


Apr 26, 2012 at 9:33 PM

Yeah... I will update the Nuget package tomorrow. Keep an eye out for that. :)

Apr 27, 2012 at 7:35 PM

Nuget package updated to