Testing model binding in ASP.NET MVC 2

The first; I know that I don’t need to test the internals of MVC, but I really need a set of tests around the data coming into our system.

How can I, hopefully, not mock the entire HTTP context, check that objectA (form assembly, dict, collection, object, etc.) does not match or does not match objectAModel?

I would not need to instantiate my controller or invoke an action. I just want to check if my new object has changed the model state.

I would just like to write

var modelState = new ModelBindingContext<objectAModel>().validate(objectA);
+5
source share
2 answers

DataAnnotations


?

DataAnnotations , .

, :

  • DataAnnotationsModelBinder ?    , , .
  • DataAnnotations?    , .
  • , ?    .
+2

, , ModelMetadata , ModelValidator .

public bool IsModelValid<T>(T model) where T : class
{
    var metaData = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(T));
    var validator = ModelValidator.GetModelValidator(metaData, new ControllerContext());
    var validationResults = validator.Validate(model);
    return 0 == validationResults.Count();
}

"" , , .

0

All Articles