Unit tests on MVC validation
Solution 1
Instead of passing in a BlogPost
you can also declare the actions parameter as FormCollection
. Then you can create the BlogPost
yourself and call UpdateModel(model, formCollection.ToValueProvider());
.
This will trigger the validation for any field in the FormCollection
.
[HttpPost]
public ActionResult Index(FormCollection form)
{
var b = new BlogPost();
TryUpdateModel(model, form.ToValueProvider());
if (ModelState.IsValid)
{
_blogService.Insert(b);
return (View("Success", b));
}
return View(b);
}
Just make sure your test adds a null value for every field in the views form that you want to leave empty.
I found that doing it this way, at the expense of a few extra lines of code, makes my unit tests resemble the way the code gets called at runtime more closely making them more valuable. Also you can test what happens when someone enters "abc" in a control bound to an int property.
Solution 2
Hate to necro a old post, but I thought I'd add my own thoughts (since I just had this problem and ran across this post while seeking the answer).
- Don't test validation in your controller tests. Either you trust MVC's validation or write your own (i.e. don't test other's code, test your code)
- If you do want to test validation is doing what you expect, test it in your model tests (I do this for a couple of my more complex regex validations).
What you really want to test here is that your controller does what you expect it to do when validation fails. That's your code, and your expectations. Testing it is easy once you realize that's all you want to test:
[test]
public void TestInvalidPostBehavior()
{
// arrange
var mockRepository = new Mock<IBlogPostSVC>();
var homeController = new HomeController(mockRepository.Object);
var p = new BlogPost();
homeController.ViewData.ModelState.AddModelError("Key", "ErrorMessage"); // Values of these two strings don't matter.
// What I'm doing is setting up the situation: my controller is receiving an invalid model.
// act
var result = (ViewResult) homeController.Index(p);
// assert
result.ForView("Index")
Assert.That(result.ViewData.Model, Is.EqualTo(p));
}
Solution 3
I had been having the same problem, and after reading Pauls answer and comment, I looked for a way of manually validating the view model.
I found this tutorial which explains how to manually validate a ViewModel that uses DataAnnotations. They Key code snippet is towards the end of the post.
I amended the code slightly - in the tutorial the 4th parameter of the TryValidateObject is omitted (validateAllProperties). In order to get all the annotations to Validate, this should be set to true.
Additionaly I refactored the code into a generic method, to make testing of ViewModel validation simple:
public static void ValidateViewModel<TViewModel, TController>(this TController controller, TViewModel viewModelToValidate)
where TController : ApiController
{
var validationContext = new ValidationContext(viewModelToValidate, null, null);
var validationResults = new List<ValidationResult>();
Validator.TryValidateObject(viewModelToValidate, validationContext, validationResults, true);
foreach (var validationResult in validationResults)
{
controller.ModelState.AddModelError(validationResult.MemberNames.FirstOrDefault() ?? string.Empty, validationResult.ErrorMessage);
}
}
So far this has worked really well for us.
Solution 4
When you call the homeController.Index method in your test, you aren't using any of the MVC framework that fires off the validation so ModelState.IsValid will always be true. In our code we call a helper Validate method directly in the controller rather than using ambient validation. I haven't had much experience with the DataAnnotations (We use NHibernate.Validators) maybe someone else can offer guidance how to call Validate from within your controller.
Solution 5
I was researching this today and I found this blog post by Roberto Hernández (MVP) that seems to provide the best solution to fire the validators for a controller action during unit testing. This will put the correct errors in the ModelState when validating an entity.
Related videos on Youtube
Matthew Groves
Something about the curly brace speaks to me. Matthew D. Groves is a guy who loves to code. It doesn't matter if it's C#, jQuery, or PHP: he'll submit pull requests for anything. He has been coding professionally ever since he wrote a QuickBASIC point-of-sale app for his parent's pizza shop back in the 90s. He currently works as a Product Marketing Manager for Couchbase. His free time is spent with his family, watching the Reds, and getting involved in the developer community. He is the author of AOP in .NET (published by Manning), a Pluralsight author, and a Microsoft MVP.
Updated on July 05, 2022Comments
-
Matthew Groves almost 2 years
How can I test that my controller action is putting the correct errors in the ModelState when validating an entity, when I'm using DataAnnotation validation in MVC 2 Preview 1?
Some code to illustrate. First, the action:
[HttpPost] public ActionResult Index(BlogPost b) { if(ModelState.IsValid) { _blogService.Insert(b); return(View("Success", b)); } return View(b); }
And here's a failing unit test that I think should be passing but isn't (using MbUnit & Moq):
[Test] public void When_processing_invalid_post_HomeControllerModelState_should_have_at_least_one_error() { // arrange var mockRepository = new Mock<IBlogPostSVC>(); var homeController = new HomeController(mockRepository.Object); // act var p = new BlogPost { Title = "test" }; // date and content should be required homeController.Index(p); // assert Assert.IsTrue(!homeController.ModelState.IsValid); }
I guess in addition to this question, should I be testing validation, and should I be testing it in this way?
-
RichardOD almost 15 yearsIsn't var p = new BlogPost { Title = "test" }; more Arrange than Act?
-
Seth Flowers over 10 yearsAssert.IsFalse(homeController.ModelState.IsValid);
-
-
Matthew Groves almost 15 yearsI like this approach, but it seems like a step backwards, or at least one extra step that I have to put in each action that handles POST.
-
Matthew Groves almost 15 yearsI like the term "ambient validation". But there must be a way to trigger this in a unit test though?
-
James Scholes almost 15 yearsI agree. But having my unit tests and the real app work the same way is worth the effort.
-
Paul Alexander almost 15 yearsThe issue though is that you're basically testing the MVC framework - not your controller. You're trying to confirm that MVC is validating your model as you expect. The only way to do that with any certainty would be to mock the entire MVC pipeline and simulate a web request. That's probably more than you really need to know. If you're just testing that the data validation on your models is setup correctly you can do that without the controller and just run the data validation manually.
-
Giles Smith almost 14 yearsSorry hadn't even checked that. All our MVC projects are in 4.0
-
kamranicus over 13 yearsARMs approach is better IMHO :)
-
Thomas Lundström over 12 yearsThanks for this! A small addendum; if you have validation that isn't coupled to a specific field (i.e. you've implemented IValidatableObject), the MemberNames is empty, and the model error key should be an empty string. In the foreach you can do:
var key = validationResult.MemberNames.Any() ? validationResult.MemberNames.First() : string.Empty; controller.ModelState.AddModelError(key, validationResult.ErrorMessage);
-
Willem Meints over 12 yearsDoing validation this way throws away some of the best parts of validation in MVC. I want to add validation on my model, not in the controller. If I use this solution I will end up with a lot of possible code duplicates with the accompanying nightmares.
-
codeulike over 12 years@W.Meints: right, but the lines of code in the above example that do the validation could also be moved to a method on the Model if you prefer. The point is, doing the validation via code rather than Attributes makes it more testable. Paul explains it better above stackoverflow.com/a/1269960/22194
-
Chad Grant over 12 yearsWhy does that need to be using Generics? This could be consumed much easier if it were defined as : void ValidateViewModel(object viewModelToValidate, Controller controller) or even better as an extension method: public static void ValidateViewModel(this Controller controller, object viewModelToValidate)
-
Andy about 12 yearsThis kind of defeats the purpose of MVC.
-
Suhas about 12 yearsThis does not work if you have got more than one validation attribute on one property. Add this line
controller.ModelState.Clear();
before the code that createsModelBindingContext
and it would work -
user3506401 about 12 yearsI agree, this should be the correct answer. As ARM says: the built-in validation should not be tested. Instead, the behaviour of your controller should be the thing that is tested. That makes the most sense.
-
user3506401 about 12 yearsI agree that ARM's answer is better. Passing in a FormCollection to a controller action is undesirable, in comparison to passing a strongly typed Model/ViewModel object.
-
Roger about 12 yearsThis is great, but I agree with Chad just get rid of the generic syntax.
-
TiMoch about 11 yearsController should be tested separately from model binding and validation. Follows both KISS and separation of concern. I am making a small series of articles on unit testing MVC components here timoch.com/blog/2013/06/…
-
Richard B almost 11 yearsI feel funny updating this, as we're now into MVC4 (with MVC5 just down the road), and my MVC3 memory is a bit rusty.. but as you go into MVC3 and MVC4, there are new calls on the controller class called ValidateModel([model]) and TryValidateModel([model]), which can be called.
-
John Saunders almost 10 yearsWhat should you do in order to test custom validation attributes? If those are being used, then one cannot "trust MVC's validation". How would you test (in the model tests, presumably) that the custom validation is working?
-
Teoman shipahi about 9 yearsHow we are going to test built-in validation then? Especially if we customized it with extra attributes, error messages etc.
-
Ibrahim ben Salah almost 9 yearsI dont agree. We still need to verify that a given model will produce the model-errors used as precondition in this test. The example code is however a perfect answer to your own defined question in 1. However it is not the answer to the initial question
-
Alin Ciocan almost 8 yearsIf anybody had the same problem as me with the "Validator", then use "System.ComponentModel.DataAnnotations.Validator.TryValidateObject" to make sure you use the correct Validator.
-
Rosdi Kasim about 7 yearsThis is not testing the model validation. Case in point, someone could (intentionally or accidentally) remove a data annotation in the model (maybe a merging error?) and this test will not fail.
-
ARM almost 7 years@RosdiKasim Then you write a test that confirms that you have said attribute on the model.