public void TryValidateModel_TestAgainstModelSuccess() { TestWidget model = new TestWidget(); Type modelType = model.GetType(); WFModelMetaData metadata = new WFModelMetaData(); WFModelMetaData metadataExpected = new WFModelMetaData(); bool actual; model.Email = "*****@*****.**"; model.MaxLengthName = "goodname"; model.RequiredName = "reqname"; //Required model.Sprockets = 6; //Good sprocket count model.Price = 0.99d; //Good price model.NoErrorMessage = "reqmsg"; //Required - no error message defined actual = WFUtilities.TryValidateModel(model, "", new WFObjectValueProvider(model, ""), metadata, new WFTypeRuleProvider(modelType)); //No Errors Assert.AreEqual(metadata.Errors.Count, 0); //Properties are not collected when an error does not exist //The reason is because there is no page to collect them for Assert.AreEqual(metadata.Properties.Count, 0); Assert.AreEqual(actual, true); }
public void TryValidateModel_TestAJAXLikeValidation() { TestWidget model = new TestWidget(); Type modelType = model.GetType(); List <string> errors = null; WFModelMetaData metadata = new WFModelMetaData(); WFModelMetaData metadataExpected = new WFModelMetaData(); bool actual; Dictionary <string, string> values = new Dictionary <string, string>(); values.Add("model_RequiredName", "reqname"); values.Add("model_MaxLengthName", "toolongofaname"); values.Add("model_Sprockets", "4"); //bad sprocket value values.Add("model_Email", "bademail"); //bad email values.Add("model_Price", "999"); //bad price // NoErrorMessage property is NOT added and should NOT be present actual = WFUtilities.TryValidateModel(model, "model_", new WFDictionaryValueProvider(values), metadata, new WFTypeRuleProvider(modelType)); errors = metadata.Errors; Assert.AreEqual(errors.Count, 4); Assert.AreEqual(errors[0], "Max length 10 characters"); Assert.AreEqual(errors[1], "Invalid number of sprockets."); Assert.AreEqual(errors[2], "Widget must have valid e-mail"); //Generated error message on custom annotation Assert.AreEqual(errors[3], "Invalid price"); Assert.AreEqual(actual, false); }
public void TryValidateModel_TestAgainstModel() { TestWidget model = new TestWidget(); Type modelType = model.GetType(); List <string> errors = null; List <string> errorsExpected = new List <string>(); WFModelMetaData metadata = new WFModelMetaData(); WFModelMetaData metadataExpected = new WFModelMetaData(); bool expected = false; bool actual; model.Email = "bademail"; model.MaxLengthName = "toolongofaname"; model.RequiredName = ""; //Required model.Sprockets = 4; //Invalid sprocket count model.Price = 999; //Invalid price model.NoErrorMessage = ""; //Required - no error message defined actual = WFUtilities.TryValidateModel(model, "", new WFObjectValueProvider(model, ""), metadata, new WFTypeRuleProvider(modelType)); errors = metadata.Errors; Assert.AreEqual(errors.Count, 6); //Generated error messages on standard annotations Assert.AreEqual(errors[0], "Widget name required"); Assert.AreEqual(errors[1], "Max length 10 characters"); Assert.AreEqual(errors[2], "Invalid number of sprockets."); Assert.AreEqual(errors[3], "Widget must have valid e-mail"); //Generated error message on custom annotation Assert.AreEqual(errors[4], "Invalid price"); //Auto-generated error message Assert.AreEqual(errors[5], "The NoErrorMessage field is required."); Assert.AreEqual(metadata.Properties.Count, 6); Assert.AreEqual(metadata.Properties[0].PropertyName, "RequiredName"); Assert.AreEqual(metadata.Properties[1].PropertyName, "MaxLengthName"); Assert.AreEqual(metadata.Properties[2].PropertyName, "Sprockets"); Assert.AreEqual(metadata.Properties[3].PropertyName, "Email"); Assert.AreEqual(metadata.Properties[4].PropertyName, "Price"); Assert.AreEqual(metadata.Properties[5].PropertyName, "NoErrorMessage"); Assert.AreEqual(actual, false); }