private static void AssertValidationError(ValidationException validationException, string propertyName, string errorMessage) { Assert.IsNotNull(validationException.ValidationErrors); Assert.GreaterThan(validationException.ValidationErrors.Count, 0); //asser the correct error Assert.AreEqual(validationException.ValidationErrors .Count(e => e.PropertyName == propertyName && e.ErrorMessage == errorMessage), 1); }
public void posting_invalid_data_to_edit_returns_view_and_adds_modelerror() { //arrange var id = 1; var name = string.Empty; var subject = "Welcome to Springfield!"; var text = "Lorem ipsum dolor sit amet."; var html = "<p>Lorem <b>ipsum</b> dolor sit amet.</p>"; var form = new FormCollection{ { "Name", name }, { "Subject", subject }, { "Text", text }, { "Html", html } }; controller.ValueProvider = form.ToValueProvider(); var ex = new ValidationException( new List<ValidationError>{ new ValidationError( "Name", "Name is a required field." ) } ); service.Expect( x => x.Update( It.IsAny<Message>() ) ).Throws( ex ); //act var result = controller.Edit_Post( id ); //assert Assert.IsAssignableFrom( typeof(ViewResult), result ); var viewResult = (ViewResult)result; Assert.AreEqual( "", viewResult.ViewName ); Assert.IsFalse( viewResult.ViewData.ModelState.IsValid ); }