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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:39:20+00:00 2026-05-26T01:39:20+00:00

I am using FluentValidation in my MVC project and have the following model and

  • 0

I am using FluentValidation in my MVC project and have the following model and validator:

[Validator(typeof(CreateNoteModelValidator))]
public class CreateNoteModel {
    public string NoteText { get; set; }
}

public class CreateNoteModelValidator : AbstractValidator<CreateNoteModel> {
    public CreateNoteModelValidator() {
        RuleFor(m => m.NoteText).NotEmpty();
    }
}

I have a controller action to create the note:

public ActionResult Create(CreateNoteModel model) {
    if( !ModelState.IsValid ) {
        return PartialView("Test", model);

    // save note here
    return Json(new { success = true }));
}

I wrote a unit test to validate the behavior:

[Test]
public void Test_Create_With_Validation_Error() {
    // Arrange
    NotesController controller = new NotesController();
    CreateNoteModel model = new CreateNoteModel();

    // Act
    ActionResult result = controller.Create(model);

    // Assert
    Assert.IsInstanceOfType(result, typeof(PartialViewResult));
}

My unit test is failing because it doesn’t have any validation errors. This should succeed because model.NoteText is null and there is a validation rule for this.

It appears that FluentValidation isn’t running when I run my controller test.

I tried adding the following to my test:

[TestInitialize]
public void TestInitialize() {
    FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure();
}

I have this same line in my Global.asax to tie up the validators to the controllers automatically…but it doesn’t appear to be working in my unit test.

How do I get this working correctly?

  • 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-26T01:39:20+00:00Added an answer on May 26, 2026 at 1:39 am

    That’s normal. Validation should be tested separately from controller actions, like this.

    And to test your controller action simply simulate a ModelState error:

    [Test]
    public void Test_Create_With_Validation_Error() {
        // Arrange
        NotesController controller = new NotesController();
        controller.ModelState.AddModelError("NoteText", "NoteText cannot be null");
        CreateNoteModel model = new CreateNoteModel();
    
        // Act
        ActionResult result = controller.Create(model);
    
        // Assert
        Assert.IsInstanceOfType(result, typeof(PartialViewResult));
    }
    

    A controller shouldn’t really know anything about fluent validation. What you need to test here is that if there is a validation error in the ModelState your controller action behaves correctly. How this error was added to the ModelState is a different concern that should be tested separately.

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

Sidebar

Related Questions

I have an MVC3 view model defined as: [Validator(typeof(AccountsValidator))] public class AccountViewModel { public
I have code like using FluentValidation; public class FreeformValidator : AbstractValidator<Freeform> { public FreeformValidator()
Following the article http://www.thekip.nl/2011/09/22/using-fluentvalidation-for-both-domain-validation-and-validation-in-mvc-projects/ for me it's not still clear where does the validation
Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I'm using FluentValidation to validate input on an MVC form. I have a checkbox.
I have the following view in ASP.net MVC 3: @model Models.CreateProjectViewModel <script type=text/javascript src=@Url.Content(~/Scripts/jquery.validate.min.js)></script>
I am attempting to use FluentValidation 2.0 with an MVC 3 project. I have
I have a MVC 3 site but am using the non-MVC FluentValidation dll. I've
I have a FluentValidation validator that I want to use to validate a booking.
I'm using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of

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.