Twitter Feed Popout byInfofru

OverrideThis.com

Adventures in .NET Software Craftsmanship!

MVC Extensibility–CMAP Code Camp

downloadThe following is the download link for the demo code and slides for my presentation on MVC Extensibility done at the Central Maryland Association of .NET Professionals Code Camp (CMAP Code Camp).  Thanks to all who attended my presentation, it was extremely fun plowing through a ton of code all across the stack of the ASP.NET MVC Framework request execution lifecycle.

 

Download Demo Code & Slides

 

Also, big thanks to Christopher Steen and Randy Hayes for hosting and allowing me to participate as a speaker in such a wonderful event.

Codes and Slides on MVC Extensibility

I have made my slides and code available from my presentation on MVC Extensibility at the MVC Conference.   Before I give out the details,  I would like to congratulate the organizers of the event for their great work on making this amazing event happen.  In particular, I would like to extend a personal thanks to Eric Hexter, and Javier Lozano.  Thanks again guys, for giving me the opportunity to speak on such an amazing digital venue.

 

The the slides and code are available at codeplex in this location.

http://mvcextensibility.codeplex.com

Presenting at MVCConf

I am excited to announce that I will be participating as a presenter at MVCConf.  MVC Conference (MVCConf) is a free virtual conference focused on one thing only, writing awesome application on top of the ASP.NET MVC applications, for more information go to http://www.mvcconf.com

 

I will be doing a presentation on MVC Extensiblity focused on plugging functionality onto your MVC application to enforce separation of concerns therefore enhancing testability. The following are the preliminary details:

 

MVC2 Extensibility

Learn the basic extensibility points of the ASP.NET MVC Framework by refactoring an existing code base and making changes that will make your application less fragile and easier to test.  The main focus will be how to leverage dependency injection, routes, action filters,  model binders, etc. to build a better all-around application.

 

Hope to see you online on the 22h of July!

MVC2 Validation and Testing – Refactored

This post is to fix a bug enhance the functionality in the original Blog Post titled “MVC2 Validation and Testing” at the following link. 
http://blog.overridethis.com/blog/post/2010/04/22/MVC2-Model-Validation-and-Testing-Scenarios.aspx


A while back I wrote about testing Controller Actions that rely on MVC2’s new Model Validation.  I had been using the technique described in the post for a while on most of my MVC2 applications without running into any significant issue until I tried to combine validators on a single property of my Model.  The following are the modified testing scenarios that would not work with the code I provided in my originally post.  Apparently, the code I wrote originally does not target all validators that are applied through Metadata.

 

namespace WebSite.Services {

    public interface ICommentService {
        void AddComment(string email, string message);
    }

}
namespace WebSite.Models {
    
    using System.ComponentModel.DataAnnotations;

    public class AddCommentRequest {
        private const string EMAIL_REGEX = 
           @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
            + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
            + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
            + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
        [Required, RegularExpression(EMAIL_REGEX)]
        public string Email { get; set; }
        [Required]
        public string Message { get; set; }
    }

    public class AddCommentResult {
        public bool Success { get; set; }
        public string Message { get; set; }
    }
}
namespace WebSite.Controllers {

    using System.Web.Mvc;
    using Models;
    using Services;

    public class CommentController : Controller {

        private readonly ICommentService service;

        public CommentController() {

        }

        public CommentController(ICommentService service) {
            this.service = service;
        }

        [HttpPost]
        public ActionResult AddComment(AddCommentRequest request) {
            if (ModelState.IsValid) {
                this.service.AddComment(request.Email, request.Message);
                return Json(new AddCommentResult {Success = true, Message = "Success"});
            }
            return Json(
                new AddCommentResult { 
                        Success = false, 
                        Message = "The 'message' is a required field." 
                });
        }
    }
}

 

The following Test validate the Model Validation functionality enforce and needless to say if we use the original ControllerExtension for validation we get less than ideal results.

 

[TestFixture]
public class CommentControllerTests {

    private string VALID_MESSAGE = "SOME MESSAGE!";
    private string VALID_EMAIL   = "someemail@yahoo.com";
    private string NOT_VALID_MESSAGE = string.Empty;
    private string NOT_VALID_EMAIL   = "someemail@@@@@@c";
    
    [Test]
    public void CommentControllerCanAddValidMessage() {
        Assert.IsTrue(this.HttpPostToAddComments(VALID_EMAIL, VALID_MESSAGE).Success);
    }

    [Test]
    public void CommentControllerCannotAddWithoutValidMessage() {
        Assert.IsTrue(this.HttpPostToAddComments(VALID_EMAIL, NOT_VALID_MESSAGE).Success);
    }    

    [Test]
    public void CommentControllerCannotAddWithoutRequiredEmail() {
        Assert.IsTrue(this.HttpPostToAddComments("", VALID_MESSAGE).Success);
    }    

    [Test]
    public void CommentControllerCannotAddWithourValidEmailAddress() {
        Assert.IsTrue(this.HttpPostToAddComments(NOT_VALID_EMAIL, VALID_MESSAGE).Success);
    }

    private AddCommentResult HttpPostToAddComments(string email, string message) {
        
        var mockOfICommentService = new Mock<ICommentService>();
        mockOfICommentService.Setup(m => m.AddComment(VALID_EMAIL, VALID_MESSAGE)).AtMostOnce();
        var model = new AddCommentRequest {Email = email, Message = message};
        var controller = new CommentController(mockOfICommentService.Object);
        var result = controller
		.CallWithModelValidation(c => c.AddComment(model), model);
        mockOfICommentService.Verify();
        return (AddCommentResult)((JsonResult) result).Data;
    }
}

 

This time around I feel like I did better research to find a suitable solution by looking at the ASP.NET MVC 2 source code and its tests.   In my original post, I had overlooked a couple of reusable tools already in place in the ASP.NET MVC source code that would have made my work a whole lot easier like the ModelMetadataProviders, and DataAnnotationsModelValidatorProvider.   Of course, there is always room for improvement, but the following is the refactored ControllerExtensions class that allows for all the previous tests to go green.

 

public static class ControllerExtensions {

    public static ActionResult CallWithModelValidation<C, R, T>(this C controller
           , Func<C, R> action
           , T model)
        where C : Controller
        where R : ActionResult
        where T : class {
        DataAnnotationsModelValidatorProvider provider = new DataAnnotationsModelValidatorProvider();
        IEnumerable<ModelMetadata> metadata = ModelMetadataProviders
			.Current
			.GetMetadataForProperties(model, typeof(T));
        foreach (ModelMetadata modelMetadata in metadata) {
            IEnumerable<ModelValidator> validators = provider
				.GetValidators(modelMetadata, new ControllerContext());
            foreach (ModelValidator validator in validators) {
                IEnumerable<ModelValidationResult> results = validator.Validate(model);
                foreach (ModelValidationResult result in results)
                    controller.ModelState.AddModelError(modelMetadata.PropertyName, result.Message);
            }
        }
        return action(controller);
    }
}

MVC2 Model Validation and Testing Scenarios

The solution described in this blog doesn't work for all scenarios.
Navigate to the following post for a better solution:
http://blog.overridethis.com/blog/post/2010/07/08/MVC2-Validation-and-Testing-e28093-Refactored.aspx

* TIP: If you don’t know what MVC2 Model Validation is you can check out this video here.

 

Lately, I have begun using ASP.NET MVC2 Model validation as much as possible and I have noticed a significant drop in the amount of plumbing code for validating a user’s request, which is making my controller action’s lighter, more readable and easier to manage. 

 

The one big drawback of using MVC2 Model Validation is that you are no longer able to test the functionality of your Controller’s action methods without having to mock or stub the validation process.  This is due to the fact that validation occurs during the Model binding process and as such is done by the ModelBinder, which doesn’t get called if you test your controller action’s by calling them directly.  We could write code to ensure that the Model binding process happens but it would be painful as we would have to mock around with the ControllerContext which is not an easy endeavor.

 

The following is my outline for a possible solution to this problem inspired by this post.  

 

First, lets establish a context to solve the problem, the following code snippet shows a Controller, Model, and Service Interface that all are direct dependencies of the CommentController we want to test.

 

Exhibit A – Very Simple Controller, Service, and Model:

namespace WebSite.Services {

   public interface ICommentService {
        void AddComment(string message);
    }

}
namespace WebSite.Models {
    
    using System.ComponentModel.DataAnnotations;

    public class AddCommentRequest {
        [Required]
        public string Message { get; set; }
    }

    public class AddCommentResult {
        public bool Success { get; set; }
        public string Message { get; set; }
    }
}
namespace WebSite.Controllers {

    using System.Web.Mvc;
    using Models;
    using Services;

    public class CommentController : Controller {

        private readonly ICommentService service;

        public CommentController() {

        }

        public CommentController(ICommentService service) {
            this.service = service;
        }

        [HttpPost]
        public ActionResult AddComment(AddCommentRequest request) {
            if (ModelState.IsValid) {
                this.service.AddComment(request.Message);
                return Json(new AddCommentResult {Success = true, Message = "Success"});
            }
            return Json(
                new AddCommentResult { 
                        Success = false, 
                        Message = "The 'message' is a required field." 
                });
        }
    }
}

 

Now, if we run the following tests the invalid request scenario would fail due to the validation not being triggered and as such the ModelState.IsValid property would always be true.

 

Exhibit B – The Tests

[TestFixture]
public class CommentControllerTests {

    private string VALID_MESSAGE = "SOME MESSAGE!";
    private string NOT_VALID_MESSAGE = string.Empty;

    [Test]
    public void CommentControllerCanAddValidMessage() {
        Assert.IsTrue(this.HttpPostToAddComments(VALID_MESSAGE).Success);
    }

    [Test]
    public void CommentControllerCannotAddValidMessage() {
        Assert.IsFalse(this.HttpPostToAddComments(NOT_VALID_MESSAGE).Success);
    }

    private AddCommentResult HttpPostToAddComments(string message) {
        
        var mockOfICommentService = new Mock<ICommentService>();
        mockOfICommentService.Setup(m => m.AddComment(VALID_MESSAGE)).AtMostOnce();
        var model = new AddCommentRequest {Message = message};
        var controller = new CommentController(mockOfICommentService.Object);
        var result = controller.AddComment(model);
        mockOfICommentService.Verify();
        return (AddCommentResult)((JsonResult) result).Data;
    }
}

 

The best option to solve this issue and keep our tests light would be to write an extension method that would allow us to call the Controller Action elegantly while adding the additional validation that needs to be stubbed out.  The following, is a possible implementation of that extension method.  One thing to note is that this implementation would only work in .NET 4.0 scenarios that are using System.Component.DataAnnotations attributes.  You could implement a similar solution in .NET 3.5 but would not have the ValidationContext or ValidationResult objects to leverage.

 

Exhibit C – The Controller Extension for validating System.Component.DataAnnotations

public static class ControllerExtensions {

    public static ActionResult CallWithModelValidation<C,R,T>(this C controller
            , Func<C,R> action
            , T model) 
        where C : Controller
        where R : ActionResult
        where T : class {

        var validationContext = new ValidationContext(model, null, null);
        var validationResults = new List<ValidationResult>();
        Validator.TryValidateObject(model, validationContext, validationResults);
        foreach (var validationResult in validationResults) {
            controller
                .ModelState
                .AddModelError(validationResult.MemberNames.First(), 
                    validationResult.ErrorMessage);
        }

        return action(controller);
    }
}

 

Having this extension method in place would allow us to rewrite just a single line of code in the tests to ensure everything is working as expected.

 

Exhibit D – The corrected test scenarios

[TestFixture]
public class CommentControllerTests {

    private string VALID_MESSAGE = "SOME MESSAGE!";
    private string NOT_VALID_MESSAGE = string.Empty;

    [Test]
    public void CommentControllerCanAddValidMessage() {
        Assert.IsTrue(this.HttpPostToAddComments(VALID_MESSAGE).Success);
    }

    [Test]
    public void CommentControllerCannotAddValidMessage() {
        Assert.IsFalse(this.HttpPostToAddComments(NOT_VALID_MESSAGE).Success);
    }

    private AddCommentResult HttpPostToAddComments(string message) {
        
        var mockOfICommentService = new Mock<ICommentService>();
        mockOfICommentService.Setup(m => m.AddComment(VALID_MESSAGE)).AtMostOnce();
        var model = new AddCommentRequest {Message = message};
        var controller = new CommentController(mockOfICommentService.Object);
        var result = controller.CallWithModelValidation(c => c.AddComment(model), model);
        mockOfICommentService.Verify();
        return (AddCommentResult)((JsonResult) result).Data;
    }
}