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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:29:58+00:00 2026-05-12T20:29:58+00:00

In my application I have a custom model binder that I set to the

  • 0

In my application I have a custom model binder that I set to the DefaultBinder in the global.asax:

 ModelBinders.Binders.DefaultBinder = new XLDataAnnotationsModelBinder();

When writing unit tests for controllers I need to make sure the controller uses the custom model binder but I don’t know how to do this.

My test looks like this:

 [Test]
 public void Details_Post_Action_Fails_To_Change_Email_Address_With_Duplicate()
 {
     // Setup
     var controller = new AccountController();
     controller.SetFakeControllerContext();

     var param = Customer.Load(30005);
     param.EmailAddress = "foo@bar.com";

     // Test
     var result = controller.Details(param);

     // Assert
     Assert.IsTrue(result is ViewResult);  // will be ViewResult if failed....
     Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
 }

With this unit test the controller ends up using the DefaultModelBinder. What can I add in this test to ensure the controller uses the custom model binder?

  • 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-12T20:29:58+00:00Added an answer on May 12, 2026 at 8:29 pm

    Scott Hanselman made a blog post related to this a while ago:

    Splitting DateTime – Unit Testing ASP.NET MVC Custom Model Binders

    The part that would interest you is at the bottom of the post under “Testing the Custom Model Binder”. Basically you instantiate a ModelBindingContext, then instantiate your Modelbinder and call Bind() on your Modelbinder passing in the ModelBindingContext you created (and the controller context if required).

    Here is another question at SO that also contains the information you need (even if you’re not using Moq):

    How to Unit Test a custom ModelBinder using Moq?

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

Sidebar

Related Questions

I have a custom application that was built to send opt-in newsletters and marketing
I have a custom class loader so that a desktop application can dynamically start
I have a custom error page set up for my application: <customErrors mode=On defaultRedirect=~/errors/GeneralError.aspx
I have an application that has created a number of custom event log sources
Let's say that I have written a custom e-mail management application for the company
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
I have an application that uses a service to create an ArrayList of custom
I have an existing Django application with a pretty typical model implementation that's backed
I'm creating a custom model binder in an Mvc application and I want to
I am building an MVC application and am designing a custom model binder for

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.