AnthonySteele / MvcRouteTester

A library for unit testing ASP MVC route tables for both Web and API Routes
Apache License 2.0
105 stars 43 forks source link

Testing controller and http method match on web routes #7

Open AnthonySteele opened 11 years ago

AnthonySteele commented 11 years ago

I took a look at testing if a web route has a particular Http method, and soon found that you can match a particular web route and pass current tests even if there is no controller to match (The test is HasRouteTests.HasRouteWithoutController() ) so not only is this not trivial, current tests could still leave you open to typos in controller names and other bugs.

I had a look at fixing this, but it seems that you have to try to instantiate the controller, and this involves the controller factory, which is something that is widely overridden. In particular, any app using an IoC container replaces the controller factory. I can try to write code that uses the registered controller factory to get the controller.

Bear in mind that the IoC container would have to be populated before factory can try to resolve controller instances. So it would test that controllers can be created, but testing all that integration has a downside too - it’s a lot more complex, error-prone setup that most tests don’t need. Integration tests are good too, but be clear about what kind of test is what and don’t mix them.

but is it worthwhile to pursue this? Will it work? has anyone had success with this kind of test? Or is it going to fragile and fiddly to set up these tests?

AnthonySteele commented 10 years ago

Widely used workaround: use the strongly typed fluent wrapper. With this method, the controller name is an actual type name, build will break if there's a missing class or typo in the class name.