Unittesting with ASP MVC: Validator.TryValidateObject returns always true

zizuu

New Member
I'm trying to validate a MVC-model with Validator.TryValidateObject. This works in production mode, but not in the unittest code.Validator.TryValidateObject give me alwasy true.The code parts: In the Controller-Constructor, I inject the Model:\[code\]private IEmailModel model; public JoggenController(IEmailModel m) { this.model = m; } \[/code\]In the action-methode I validate the model, works fine in production mode, isValid is true or false: \[code\]public ViewResult AddEmail(String email) { model.Email = email; var context = new ValidationContext(model, null, null); var list = new List<ValidationResult>(); var isValid = Validator.TryValidateObject(model, context, list, true); //...\[/code\]Now I will test my AddEmail() and use this testcode: \[code\]var emailModel = new Mock<IEmailModel>(); emailModel .Setup(p => p.Email) .Returns("xxx"); var controller = new Controller(emailModel.Object); //should Faking ModelState.IsValid = false controller.ModelState.Add("testError", new ModelState()); controller.ModelState.AddModelError("testError", "test"); var result = controller.AddEmail("xxx") as ViewResult; //... \[/code\]If I debug the testcode, var isValid = Validator.TryValidateObject returns always true.Whats the Problem?Thanks
 
Back
Top