Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 281263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:11:18+00:00 2026-05-12T05:11:18+00:00

Does anyone know how to generate a test for the xVal, or more the

  • 0

Does anyone know how to generate a test for the xVal, or more the the point the DataAnnotations attributes

Here is some same code that I would like to test

[MetadataType(typeof(CategoryValidation))]
public partial class Category : CustomValidation
{
}

public class CategoryValidation
{
    [Required]
    public string CategoryId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    [StringLength(4)]
    public string CostCode { get; set; }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:11:18+00:00Added an answer on May 12, 2026 at 5:11 am

    Well, testing it should be pretty easy. For me, using NUnit, its like this:

        [Test]
        [ExpectedException(typeof(RulesException))]
        public void Cannot_Save_Large_Data_In_Color()
        {
    
            var scheme = ColorScheme.Create();
            scheme.Color1 = "1234567890ABCDEF";
            scheme.Validate();
            Assert.Fail("Should have thrown a DataValidationException.");
        }
    

    This assumes you have a validation runner for DataAnnotations already built and a way to call it. If you don’t here is a really simplistic one I use for testing (which I nicked from Steve Sanderson’s blog):

    internal static class DataAnnotationsValidationRunner
    {
        public static IEnumerable<ErrorInfo> GetErrors(object instance)
        {
            return from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>()
                   from attribute in prop.Attributes.OfType<ValidationAttribute>()
                   where !attribute.IsValid(prop.GetValue(instance))
                   select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);
        }
    }
    

    In the small sample above I call the runner like so:

    public class ColorScheme
    {
         [Required]
         [StringLength(6)]
         public string Color1 {get; set; }
    
         public void Validate()
         {
             var errors = DataAnnotationsValidationRunner.GetErrors(this);
             if(errors.Any())
                 throw new RulesException(errors);
         }
    }
    

    This is all overly simplistic but works. A better solution when using MVC is the Mvc.DataAnnotions model binder which you can get from codeplex. Its easy enough to build your own modelbinder from DefaultModelBinder, but no need to bother since its already been done.

    Hope this helps.

    PS. Also found this site which has some sample unit tests working with DataAnnotations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how to generate an XSD using LinqToXml? I can't find any
Does anyone know a way to auto-generate database tables for a given class? I'm
Does anyone know if it's possible to generate powerpoint ppts within ColdFusion? I can't
Does anyone know of a simple way to compare two strings together to generate
Does anyone know of an Eclipse plug-in or method to get Eclipse to generate
Does anyone know of a good Eclipse POJO generator? The generate getter\setters and constructor
Does anyone know why NHibernate generates a field named 'elt' of type int for
Does anyone know why there is no respond_to block for generated edit actions? Every
Does anyone know what advantages (memory/speed) there are by using a class generated by
Does anyone know how to get IntelliSense to work reliably when working in C/C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.