Pro ASP.NET MVC 3 Framework (Expert's Voice in .NET)

Author: Adam Freeman, Steven Sanderson
All Stack Overflow 17


by anonymous   2017-08-20

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)

  1. Realize you need a new feature for your MVC application
  2. Write a test that fails (because there is no actual code at this point)
  3. Write the new feature you were wanting to add to your application
  4. Run your test, if the test passes, move on to 5, if it fails, edit the code and repeat 4
  5. Refactor as needed and re-run the test to ensure that everything passes

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):

  • Run your tests frequently
  • Make sure your tests all pass after they've passed once in the past (sounds obvious, but some people are crazy)
  • Make sure you can run your tests easily and quickly, preferably one or two clicks and the tests are run. This will allow you to more easily run them frequently - this is what the tests are here for.
  • Stick to it, it is easy to get lazy and just say, "yeah, I know this part will work, no need to test it," but when you are looking at that code weeks/months/years from now and you are not as closely connected to it, you may carelessly change something and run your project's tests (since it was developed using a TDD process) and notice everything passes. But, since there wasn't a test for the part you changed, you have no evidence that the code is all working as planned. It sounds simple, but think of it as a forgotten 'broken window' that can lead to other problems.

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.

by anonymous   2017-08-20

You can use the partial validation technique to modify the validation results. This example will discard any errors for the Email field.

public class DontValidateEmailAttribute : ActionFilterAttribute {

  public override void OnActionExecuting(ActionExecutingContext filterContext) {
    var modelState = filterContext.Controller.ViewData.ModelState; 
    var incomingValues = filterContext.Controller.ValueProvider;

    var key = modelState.Keys.Single(x => incomingValues.Equals("Email"));    


and apply this attribute to your Edit Controller.

I learnt this technique from Steve Sanderson's Pro ASP NET MVC 3. He uses the technique to validate a model that has required fields but the data entry is a multistep wizard. If the value has not been returned in the form post, he removes the errors for that property.

by anonymous   2017-08-20

I think making this field nullable and also making it Required is the best shot for now. As the book states, . Otherwise, you can write your own attribute, make the validate method return false when passed in value = default(T)