I have a custom asp.net mvc class validation attribute. My question is how can I unit test it? It would be one thing to test that the class has the attribute but this would not actually test that the logic inside it. This is what I want to test.
[Serializable] [EligabilityStudentDebtsAttribute(ErrorMessage = "You must answer yes or no to all questions")] public class Eligability { [BooleanRequiredToBeTrue(ErrorMessage = "You must agree to the statements listed")] public bool StatementAgree { get; set; } [Required(ErrorMessage = "Please choose an option")] public bool? Income { get; set; }
.....removed for brevity }
[AttributeUsage(AttributeTargets.Class)] public class EligabilityStudentDebtsAttribute : ValidationAttribute { // If AnyDebts is true then // StudentDebts must be true or false public override bool IsValid(object value) { Eligability elig = (Eligability)value; bool ok = true; if (elig.AnyDebts == true) { if (elig.StudentDebts == null) { ok = false; } } return ok; } }
I have tried to write a test as follows but this does not work:
[TestMethod] public void Eligability_model_StudentDebts_is_required_if_AnyDebts_is_true() { // Arrange var eligability = new Eligability(); var controller = new ApplicationController(); // Act controller.ModelState.Clear(); controller.ValidateModel(eligability); var actionResult = controller.Section2(eligability,null,string.Empty); // Assert Assert.IsInstanceOfType(actionResult, typeof(ViewResult)); Assert.AreEqual(string.Empty, ((ViewResult)actionResult).ViewName); Assert.AreEqual(eligability, ((ViewResult)actionResult).ViewData.Model); Assert.IsFalse(((ViewResult)actionResult).ViewData.ModelState.IsValid); }
The ModelStateDictionary
does not contain the key for this custom attribute. It only contains the attributes for the standard validation attributes.
Why is this?
What is the best way to test these custom attributes?
A simple generic way to test Custom Attributes with NUnit. This test class contains two test methods which are identified by the NUnit [Test] attribute.
To create a custom validation attributeIn Solution Explorer, right-click the App_Code folder, and then click Add New Item. Under Add New Item, click Class. In the Name box, enter the name of the custom validation attribute class. You can use any name that is not already being used.
ValidationContext(Object) Initializes a new instance of the ValidationContext class using the specified object instance. ValidationContext(Object, IDictionary<Object,Object>) Initializes a new instance of the ValidationContext class using the specified object and an optional property bag.
Attribute Validation is an advanced VLDB property that is hidden by default. For information on how to display this property, see Viewing and Changing Advanced VLDB Properties.
Your attribute EligabilityStudentDebtsAttribute
is just a standard class, like everything else, just unit test the IsValid()
method. If it works OK, trust to Framework that attribute works OK.
So:
[Test] public void AttibuteTest() { // arrange var value = //.. value to test - new Eligability() ; var attrib = new EligabilityStudentDebtsAttribute(); // act var result = attrib.IsValid(value); // assert Assert.That(result, Is.True) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With