0

We have an ASP.NET WebForms application that we're going to convert to be ASP.NET MVC application - one of the reasons is so that we may take full advantage of Unit Testing and TDD.

Our current WebForms application makes heavy use of HttpModules. We're currently debating whether we should stick with HttpModules or use Global Filters (any advice here would be great).

However, with my few "hello world" test MVC applications, I've not worked out how to get the HttpModule (or indeed the Global Filter) code to fire when Unit testing.

Maybe I'm wrong, but it seems to me that I need this to be part of my unit-testing (integration testing) otherwise it can't be a true representation of what's going on in my Production code.

Any guidance would be most welcome.

Thanks

Griff

PS - I added the following after the initial responses to my question.

Simple made-up Use Case

In Production

  1. Browser makes a Request for a Controller Method
  2. HttpModule fires (or Global Filter) - this sets a static Guid property to have a value.
  3. The controller method takes this Guid value and uses it in its subsequent logic
  4. Correct result is then returned to the browser

In Unit Testing

  1. Unit test calls the same Controller Method
  2. The HttpModule (or Global Filter) does not fire - the static Guid property has a value of Guid.Empty
  3. The controller method takes this Guid value, but throws an error because it was an Empty Guid
  4. Unit test fails

My thoughts are:

  • if a Controller relies upon an HttpModule (or Global Filter) to run first, then the HttpModule is a dependency and therefore the results of it's action must be Stubbed for the test to be a true Unit Test.
  • If the test is to include the effect of the HttpModule (or Global Filter) to run, then this would be an Integration test (though it's still not clear yet how to get the HttpModule or Global Filter to run as part of the Integration test)
  • The HttpModule (or Global Filter) should have their own set of Unit Tests

I think this is the approach I'll take, but if anyone could suggest how I get my Integration test (as described above) to work then I'd be most grateful.

The problem as I see it is that my TEST project becomes the "start up" project when running my tests, so the HttpModules (defined in the ASP.NET MVC's web.config file) and the Global Filters (defined in the ASP.NET MVC's global.asax file) will not run because the web.config and global.asax files are not executed. So the question remains: how do I get these to run in my Integration test?

Thanks everyone

Griff

DrGriff
  • 4,394
  • 9
  • 43
  • 92

3 Answers3

4

You shouldn't be testing to check if the global filter fires or not. The mechanism that fires the code isn't your code, it's part of the framework so the framework team is supposed to test that.

Instead you should simply be testing to make sure that the global filter has been applied.

Hattan Shobokshi
  • 687
  • 5
  • 13
  • Hi Hattan - thanks for your response which confirms what Justin said. However, I've expanded my initial post with a made-up Use Case and I'd also appreciate your thoughts on that. Griff – DrGriff Apr 05 '12 at 08:45
2

Whether or not to call a HttpModule or Global Filter is handled by the Framework. You shouldn't be worried about Unit Testing things that are handled by the Framework. Your Unit Tests should only test that your HttpModule or Global Filter behave properly when called.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Hi Justin - thanks for your response. I added a Use Case to my original post to further explain the situation - would appreciate your feedback. Griff – DrGriff Apr 05 '12 at 08:43
1

I would mock the http modules and repositories to unit test the controllers and services, in my view that's how you unit test. the global filters and httpmodules you can test it with end to end tests. Hope it helps.

hopper
  • 4,230
  • 8
  • 36
  • 49
  • Hi Teahupoo - the ASP.NET MVC's web.config file determines which HttpModules will fire, just as the ASP.NET's Global.ASAX file determines which Global Filters will fire. However, when running my tests, the ASP.NET MVC project is NOT the startup project, it's now effectively a library assembly that's called by my TEST project, so it's web.config and global.asax files are not executed. So, the question remains - how to get these to run in my Integration Test? – DrGriff Apr 05 '12 at 16:22
  • Do you need to test the controllers? The idea is to create a service layer and test only the service layers without needing to test controllers. – hopper Apr 10 '12 at 15:57
  • Our aim is to try to get 100% code coverage in our tests and for some of out Integration tests to include the controllers (although admittedly the controllers should contain very little logic). – DrGriff Apr 11 '12 at 06:26