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.
- Use a JS testing framework like Jasmine to test your JavaScript.
Do you have any other testing tips you want to share for testing ASP.NET MVC applications?
Community comments
One Suggestion: SpecsFor.Mvc!
by Matt Honeycutt,
Re: One Suggestion: SpecsFor.Mvc!
by Roopesh Shenoy,
One Suggestion: SpecsFor.Mvc!
by Matt Honeycutt,
Your message is awaiting moderation. Thank you for participating in the discussion.
I'm obviously biased, but I think you left out one of the best tools for testing ASP.NET MVC applications: SpecsFor.Mvc! It enables you to write acceptance tests that perform complex browser automation using simple, unit-test like syntax. You can check it out at specsfor.com/SpecsForMvc and on my blog at trycatchfail.com/blog/post/Using-SpecsForMvc-In....
Re: One Suggestion: SpecsFor.Mvc!
by Roopesh Shenoy,
Your message is awaiting moderation. Thank you for participating in the discussion.
Thanks for the suggestion Matt - definitely looking at it.
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....