I have written my first unit tests for an ASP.NET MVC web application. All works fine and it is giving me valuable information, but I can\'t test errors in the view model. T
I found this solution: SO: Validation does not work when I use Validator.TryValidateObject combined with the solution @Kenneth provided:
[TestMethod]
public void test_validation()
{
var sut = new POSViewModel();
// Set some properties here
var context = new ValidationContext(sut, null, null);
var results = new List();
TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(POSViewModel), typeof(POSViewModel)), typeof(POSViewModel));
var isModelStateValid = Validator.TryValidateObject(sut, context, results, true);
// Assert here
}
If you have a class library with all you resources in, don't forget to reference it in your test project.