If you are spelled correctly, you do not need to have a real context, a real session, cookies, etc. The MVC framework by default provides an HttpContext that can be mocked / drowned out. I would recommend using a mocking framework like Moq or Rhino Mocks and creating a MockHttpContext class that will create a mock context with all the properties you need to test against customization. Here's a mock HttpContext that uses Moq
There are a few things you could add, for example, to the HTTP Headers collection, but hopefully it demonstrates what you can do.
To use
var controllerToTest = new HomeController(); var context = new MockHttpContextBase(controllerToTest); // do stuff that you want to test eg something goes into session Assert.IsTrue(context.HttpContext.Session.Count > 0);
Regarding membership providers or other suppliers, you have come across something that can be difficult to verify. I would be distracted from using the provider for the interface, so that you can provide fake for the interface when testing a component that relies on it. You will still have a problem testing the specific implementation of the interface that the provider uses, but your mileage may vary depending on how far you want / should go in terms of unit testing and code coverage.
source share