Unit Testing ASP.NET MVC3 Applications (with NHibernate) -


i'm starting first mvc3 application, , i'm not sure how unit-test it. planning break out helper classes (static helpers, usually) separate assembly, model classes, test them nunit.

so i'm ok on helper classes; how test model classes (considering they're annotated nhibernate , tied database), , how can test views , controllers?

what specific tools , techniques need test nhibernate-bound models, asp.net views , controllers? i'm not sure. nunit solves of problem.

edit: here's samples of code -- i'm not on dev machine right now, don't have real code show-case.

specific questions:

  • how can test saving models without saving main/production database
  • scope testing views; should test fields exist? validation error messages?
  • controllers: scope testing. should test actions touch , deform database data expected (eg. /get/id gets object; /delete/id deletes object)?

you can there various kinds of tests, need apply them wisely depending on going test:

  • use unit test test controllers, or business logic, without hitting database.

  • use integration testing running on in-memory database (which nhibernate supports , easy setup). can make sure scenario works, e.g. using valid scenario, business logic working, controller pass data persistence mechanism , goes correctly database.

  • you can use ui testing using frameworks such selenium needed, because not easy 2 previous types of tests, , become hard maintain , fragile.

it best practice keep view (ui) thin, , test other layers behind ui, testing ui not worth hassle.


Comments