So the controller context depends on some asp.net internals. What are some ways to cleanly mock these up for unit tests? Seems like its very easy to clog up tests with tons of setup when I only need, for example, Request.HttpMethod to return "GET".

I've seen some examples/helpers out on the nets, but some are dated. Figured this would be a good place to keep the latest and greatest.

I was considering to do this. But only needed the mock for the database connection. Instead of testing the database mapping I moved the function to a regular class and test just that function, without database connection.
–
MrFoxAug 15 '13 at 7:43

The procedure for this seems to have changed slightly in MVC2 (I'm using RC1). Phil Haack's solution doesn't work for me if the action requires a specific method ([HttpPost], [HttpGet]). Spelunking around in Reflector, it looks like the method for verifying these attributes has changed. MVC now checks request.Headers, request.Form, and request.QueryString for a X-HTTP-Method-Override value.

This worked for me, however in MVC2 RC i also had to add the following: request.Setup(r => r.Files).Returns(new Mock<HttpFileCollectionBase>().Object); otherwise i get a nullreferenceexception
–
Hugo ZapataFeb 9 '10 at 22:10

The best way we have found - using ASP.NET MVC on a real project - is to abstract the HttpContext to an IWebContext interface that simply passes through. Then you can mock the IWebContext with no pain.