I realize this is a duplicate of about 20 different posts, but none of them are specific to MVC4, and none that I've seen really answer all of my questions. So far my first foray into the world of TDD has been frustrating to say the least. Most of what I've tried to do seems incompatible with MVC 4 or next to impossible without using poorly documented third party libraries I don't quite understand yet.
What I want to be able to do, is write a tests that will test my Controller Actions, The Model they're passing and The View the action is sending the model to. I want to test if the view exists, I want to test if the model being passed is the right type for the view, I'd like some way to test if it will process properly. I also want to be able to test my routes. And testing Authentication filters?
I want a way to unit test ASP.Net MVC that will leave very little to chance.
Testing the Model output of an Action seems easy enough, but testing the views has been next to impossible.
So here's my list of questions:
I'd prefer to use the built in Visual Studio test projects, but if I must use NUnit, I must. I just need to make sure it gets done properly.
Thank you in advance for your responses.
EDIT: I also couldn't get NUnit working with my MVC4 app because of some incompatibly with the version of .NET one of the assemblies was compiled in.
We create the data access logic in a separate class, or set of classes, called a repository, with the responsibility of persisting the application's business model. As the Repository Pattern is useful for decoupling entity operations from presentation, it allows easy mocking and unit testing.
thanks! unit tests do not run in the "MVC environment." They will run within the scope of the test runner, be that nunit, resharper,....
no. 4: This is a hard question. How does one test anything thoroughly? Personally, I don't really test the views, other than with the 3 major browsers and my two eyes, as it's hard to test a website and all it's components without actually using it. You have JavaScript firing, CSS stylizing, and it looks different across different browsers. So, to me, it seems like testing the view that thoroughly is a minor part of the overall usability of your site. If you're developing a simple table based report of financial data, test that data hard. If your view is a the base for a fancy Ajax site, maybe don't test the HTML so much as the experience. I know it's not an easy, cut-and-dry answer, but the acceptable level of coverage always involves trade offs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With