Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

If I have a method taking input and giving output, it's a no-brainer that tests should be written. But what about things like validation rules?

For example, I add a validation rule that a certain property must be at least 5 characters long. Should I then write a UI test to check for an error message when the form field is empty? Assume that the validation library itself works fine, the only thing I do is add a one-liner to specify this rule.

I'm leaning towards no, because I'd only be duplicating the validation rules, and if the rules itself are set up incorrectly, the test will probably also be incorrect. However, I'd very much like to hear people's opinion and experience on this.

share|improve this question

4 Answers 4

up vote 7 down vote accepted

Absolutely, including if the underlying library (business layer) works fine.

The reason is that there may be a lot of things going wrong between the business layer and the UI when it comes to validation. In the first case, you can simply throw an exception. In the second case, you have to process the exception, react to it (for example by cancelling the processing or doing or not doing a redirection, displaying a localized message, etc.)

Imagine the validation of an e-mail address. The business layer detected that "someone.somewhere.com" is not a valid e-mail address. The expected behavior is to:

  1. Stop processing,
  2. Stop redirection,
  3. Show a message to the user,
  4. Highlight the e-mail field in red.

What if:

  • The processing is not stopped?
  • The invalid e-mail is simply replaced by a null string?
  • The redirection is still done?
  • The message is not shown?
  • The message is wrong?
  • Another error message for an other field is shown together with the expected message?
  • The message is correct is some cultures, but not in others?
  • The message is too long in some cultures for the area?
  • The e-mail field is not highlighted?
  • Another field is highlighted together with the e-mail field?
  • The e-mail field is empty, i.e. the previous input is discarded?
  • All previous input of the form is discarded?
share|improve this answer
    
The four items in the expected behaviour list are all taken care of by third-party libraries. For example, if I add RuleFor(item => item.Name).NotEmpty(); to my code, everything else is done for me. Processing is stopped, redirection is stopped, a message is shown and the field is highlighted in red without any action on my side. –  Stijn Jul 26 '13 at 13:31
1  
If everything is done for you, then you don't have to test anything. But you should be sure that everything is done for you; also see programmers.stackexchange.com/q/203408/6605 –  MainMa Jul 26 '13 at 13:39
    
So it comes down to how much I trust my libraries then. In this case I trust them, they're open source and unit tested themselves. The link is interesting too. –  Stijn Jul 26 '13 at 13:43

If you don't write a test to verify that the validation rule is actually functioning how to do know that it worked?

There are customer requirements / user stories that state for the input to be valid it must by 5 characters or longer, have no double letters and no more than 2 numbers. If the user doesn't meet these standard you warn them, reject the answer, or quit the program; depending on the requirements. So of course you should test these conditions.

share|improve this answer
    
My concern is that when I misinterpret the customer requirements while writing the validation rule, I'll probably also misinterpret them when writing a test for it. –  Stijn Jul 26 '13 at 13:33
    
The tests allow you to make sure that your programming produced code that performed as you expected. It also allows you to make sure that when a code change is made that the current tests react as expected. –  mhoran_psprep Jul 26 '13 at 13:38
    
See my comments to @MainMa's answer. Your answer itself is still useful in case one can't trust his/her libraries, so +1. –  Stijn Jul 26 '13 at 13:45

It'll only take a few lines of code to test it, and that will 1 - give you confidence that the spec is still being met if you change the code, and 2 - make the tests a better documentation of the spec. Remember that unit tests are documentation and regression tests, not just proof that your first implementation works.

share|improve this answer

YES you absolutely should test your validation. Then you should test that your UI responds to it correctly as a separate test.

Also if you use regular expressions then a great tool to help you catch unexpected validation errors (or false positives) is Rex as this is quite good at exercising all the paths.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.