I find myself now with an EF Code First DbContext and entity classes, which I would prefer to exclude from unit testing right now, as Code First is not a critical requirement, and I would not unit test DB First generated classes, but above that I have a repository library, which are all copy and paste copies of one template, and then some view modes and controllers.
I would now like to adopt a TDD approach for the way forward, and add unit tests for view models that are not pure (property only) DTO’s, and my action methods. Am I going in the right direction, with the right coverage in mind? Then, how do I unit test actions? Some pointers to resources and tutorials would be nice.
Ideally, in a perfectly TDD environment you would have tests for absolutely everything in your code – but some may consider that dogmatic and you don’t see a need to test your code-first-generated-database, so don’t do it 🙂 . You’re the developer, do what you want.
I understand your feelings on the above, but if you want to develop in a true TDD style, you need to write a test before you code anything, then code to get the ‘green light’ on the test.
The basic workflow would be as follows (starting on anything you wanted to add to your app past your
DBContext)You will have a lot of tests, but you will have a very sound application as long as you are diligent about maintaining those tests.
Keep a few things in mind when using TDD with MVC (or likely any development architecture):
As far as writing tests specifically for MVC actions, I highly recommend this tutorial from MSDN and this book: Pro ASP.NET MVC 3 Framework (which is an overall great resource for MVC3, including TDD for MVC3) by Freeman and Sanderson.