Unit Testing Data Validation with MVC

Published on May 11, 2015 by Jamie Munro

Problem


You've added some data validation and you want to test it out in action. The data validation can be standard data annotations, custom data attributes, or data validation implemented via an IValidatableObject.

Solution


Unit testing data validation can be accomplished by creating a ValidationContext in conjunction with the Validator helper class. To demonstrate how this is accomplished, I will use the ValidationModel class I created in the Removing Data Validation from MVC Controllers. As you may recall this class contained an IValidatableObject that ensured if the boolean FixedQuantity was true, the MaxQuantity property must be null.

Read more >

Tags: validation | ASP.NET | mvc | Testing | unit testing | ivalidatableobject | validationcontext

Speeding up my unit tests

Published on Apr 28, 2014 by Jamie Munro

I hope this isnít too extremely obvious, but I found that I had to take a step back and re-examine my unit tests to find this simple improvement to speed up my unit tests.

When I wrote about how Iím hooked on test-driven development (TDD), the example in that post was too simple and time savings are not noticed.† However, letís dive in to something a little deeper where we have a full class to test oppose to a single internal function.

Read more >

Tags: ASP.NET | Theory | fizzbuzz | tdd | Testing | testing