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

  • SEARCH
  • Home
  • 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
  • 3 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 if there is a way to generate different code in the
Does anyone know a C# framework that can generate public/private keys, X.509 certificates and
Does anyone know of one? I need to test some upload/download scripts and need
Does anyone know of an app that would generate an xml file filled out
Does anyone know of a tool that can inspect a specified schema and generate
Does anyone know how facebook connect buttons generate the pop-up window that you use
Does anyone know why the following random distributions of matrices generate different plots? (This
Does anyone know of any websites, or (preferably) downloadable packages that you can use
A rather specific question, does anyone know an existing piece of code on how
Does anyone know of a free or trial version to test an .net web

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.