ASP.NET MVC Testing Tips and Tricks
There are several components in an ASP.NET MVC 3 web app – models, controllers, route-handlers, views, html-helpers, client-side code etc. Most of these can be unit-tested, others need integration tests, and several good practices can help you keep your tests more maintainable and avoid making them brittle.
Here are some resources -
- Along with your actions you can also scaffold the unit tests with MvcScaffolding.
- Use custom HTML Helpers instead of writing a lot of logic in your views – html helpers can be unit tested easily. Also avoid hard-coding html elements when testing custom HTML-helpers.
- Use BDD frameworks like Specflow to write end-to-end acceptance tests
- It is easy to break existing routes when adding a new route higher up the routing table – you can unit test your routing table to avoid this
- Turn on compilation for your views. This is not exactly unit testing, but it does help in identifying bugs that would otherwise be seen only on runtime.
- Use WaTiN for testing your UI, if they are not very volatile. Alternatively, you can also make your integration tests a bit flexible in accommodating pure UI design changes by using Page Objects.
Do you have any other testing tips you want to share for testing ASP.NET MVC applications?
One Suggestion: SpecsFor.Mvc!
Re: One Suggestion: SpecsFor.Mvc!
One concern I have is how do you reuse these test-steps? One of the greatest features in BDD frameworks seems to be that you can reuse your test steps in multiple test cases making them more efficient. Look here for instance - nbehave.codeplex.com/wikipage?title=Using%20NBe....
Shane Hastie on Distributed Agile Teams, Product Ownership and the Agile Manifesto Translation Program
Shane Hastie Apr 17, 2015