3

I have a little alternative MVC framework built atop ASP.Net(though not strictly tied to it). One of the things I mess around with a whole lot is HttpContext instances and HttpContext.Current. Recently, I've went to trying to put in a few unit tests for my framework and found that HttpContext is notoriously hard to test against. Because my application isn't necessarily tied to ASP.Net, I've tried reimplementing a more basic version of HttpContext, HttpRequest, and friends, but this is a massive undertaking for the shear amount of information that must be contained in these classes.

What exactly should I do? Find a work around for testing with HttpContext or restructure my code to not explicitly depend on it so much? Or is there a better way? Is using HttpContext directly a lot a bad thing?

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • This question seems useful: http://stackoverflow.com/questions/9624242/setting-the-httpcontext-current-session-in-unit-test – JayC Oct 28 '12 at 03:10

1 Answers1

2

I completely understand your pain. I've spent many hours with this an ASP MVC 2.

During my own struggles I found various discussions on unit testing versus integration testing. I can understand the distinction, but I try to do integration testing whenever possible in an environment as close to production as I can get. There's a good chance I'll be stapled to the wall for saying this; I'm not afraid.

I tried several different ways to mock the various classes needed when unit testing, both open source and with trials of TypeMock and Isolator. In the end I found an article specifically on integration testing and it solved most of the issues I was trying to overcome. See here:

http://blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/

I can't really speak as to how you should structure your code. From what I've seen in the ASP MVC source, I don't see why HttpContext shouldn't be used, even though it's a real pain to test against.

Does this help at all? I would be happy to share some samples of the various solutions I've tried if you're interested.

  • Well, some portions of my framework for instance take in a `HttpContext` as an argument... or use `HttpContext.Current` in some capacity. I'd much rather be able to unit test these portions as opposed to only integration testing – Earlz Oct 28 '12 at 04:35