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 8554581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:54:56+00:00 2026-06-11T14:54:56+00:00

In my unit tests, I would like to force the validation of a code

  • 0

In my unit tests, I would like to force the validation of a code first POCO that has DataAnnotations on it.

The MVC framework must be doing it behind the scenes, and basically I would like to know how, so I can hopefully make use of it.

  • 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-06-11T14:54:57+00:00Added an answer on June 11, 2026 at 2:54 pm

    The MVC framework bust be doing it behind the scenes, and basically I
    would like to know how, so I can hopefully make use of it.

    It’s the default model binder that is responsible for invoking the validation of the model once it has bound it from the request values.

    You could invoke the validation process manually by using a ValidationContext.

    Let’s suppose that you have a model:

    public class Foo
    {
        [Required(ErrorMessage = "the Bar is absolutely required")]
        public string Bar { get; set; }
    }
    

    and then you could unit test it:

    [TestMethod]
    public void The_Bar_Is_Required()
    {
        // arrange
        var foo = new Foo();
        var results = new List<ValidationResult>();
        var context = new ValidationContext(foo, null, null);
    
        // act
        var actual = Validator.TryValidateObject(foo, context, results);
    
        // assert
        Assert.IsFalse(actual);
    }
    

    Alternatively to using DataAnnotations you could use FluentValidation.NET to perform validation on your view models. It integrates nicely with ASP.NET MVC and it allows you to unit test your validators in a very elegant way.

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

Sidebar

Related Questions

I have some Objective-C [i-os] code that I would like to run unit tests
In order to reduce code duplication I would like to generate unit tests programatically
Currently I have some unit tests that I would like to be executed during
I would like to know how to write unit tests for a module that
I'm retroactively documenting and writing unit tests for some C# code. I would like
I would like to migrate all my unit tests from NUnit to MSTest. In
I would like to accomplish two things during my build process: Run unit tests
I'm writing some unit tests for a Python library and would like certain warnings
I have the following actor for which I would like to create unit tests
What would a simple unit test look like to confirm that a certain controller

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.