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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:56:56+00:00 2026-05-20T04:56:56+00:00

I have an ASP MVC 3 application and in my Model I have implemented

  • 0

I have an ASP MVC 3 application and in my Model I have implemented IValidatableObject.

When my controller posts for a create or edit, I obviously only want to save the model if it is valid.

I see many blogs and posts and answers that say something like

if(!ModelState.IsValid)
{
      return View();
}

My question. Why is it that ModelState.IsValid is always true in a unit test on the Controller?

Example:

[Test]
public void InValidModelsAreNotAdded()
{
    var invalidModel = new MyModel() { SomeField = "some data", SomeOtherField = "" };

    var result = _controller.Submit(invalidModel);

    _repository.AssertWasNotCalled(r => r.Add(Arg.Is.Anything));

}

Model code:


public class MyModel : IValidatableObject
{
    public string SomeField { get; set; }
    public string SomeOtherField { get; set; }

    public IEnumerable Validate(ValidationContext validationContext)
    {
        if(string.IsNullOrWhiteSpace(SomeOtherField))
        {
            yield return
                new ValidationResult("Oops invalid.", new[] {"SomeOtherField"});
        }
     }
}

The AssertWasNotCalled always fails this test.

I stepped through the test and noticed that the ModelState.IsValid is true for this test. It is as if the IValidatableObject.Validate is not being invoked. It seems to work when I run the project, but thats not much of a way to test drive an application.

Also, I realize I could use the [Required] attribute for my example, but my real code has much more complex validation to it.

Thoughts?

  • 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-20T04:56:56+00:00Added an answer on May 20, 2026 at 4:56 am

    It’s true because you haven’t called anything which sets it false.

    This normally happens during binding, but since you just pass the model directly in the test you skip that altogether.

    If you’re trying to test validation, do that directly. If you’re trying to test the error path in your controller, your test’s arrange can call _controller.ModelState.AddModelError( //...

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

Sidebar

Related Questions

In an ASP.NET MVC application where I have the following model.. public class EventViewModel
I have implemented a repository pattern in my asp.net mvc web application... But i
i have asp.net mvc application where my model has a relation like Question can
I have an ASP.Net MVC application with a model which is several layers deep
I have created a class in my asp.net MVC 3 application's Model folder and
I have ASP MVC 3 application. I created WCF REST JSON Service and added
I have an ASP.NET MVC application. I am having multiple drop-down list in my
I have an ASP.NET MVC application that uses Fluent NHibernate and AutoMapper. I am
I have a ASP.NET MVC application and I have a few entries on the
I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This

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.