10

I'm implementing the Data Validation Validators as shown here:

http://www.asp.net/learn/mvc/tutorial-39-cs.aspx

This works great at runtime, but how can I Unit Test to verify if I say attribute [StringLength(10)], an error is returned?

2 Answers 2

13

Brad Wilson describes it pretty well in a blog post from a while ago (you'll have to scroll down a bit). Basically, you write tests where you use reflection to make sure that the proper attributes are applied, and then trust the framework to do its job on adding errors. After all, someone else tested the Data Annotation Validators before they were published - you just have to make sure you use them right =)

Sign up to request clarification or add additional context in comments.

Comments

9

This post by Villecoder is the unit testing solution I'm using. It also allows you unit unit test custom annotations

http://villecoder.com/2010/04/23/unit-testing-custom-data-annotations/

2 Comments

You could extend your ModelBase class to test everything and not just either class-level validators or property-level ones... You should test the whole model state all together as well. It would involve a bit more complex code, but it would be the actual model validation unit test model base class.
Broken link. 😟

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.