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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:03:57+00:00 2026-05-14T23:03:57+00:00

I’m developing a project using BDD/TDD techniques and I’m trying my best to stay

  • 0

I’m developing a project using BDD/TDD techniques and I’m trying my best to stay the course. A problem I just ran into is unit testing the DefaultModelBinder. I’m using mspec to write my tests.

I have a class like this that I want to bind to:

public class EmailMessageInput : IMessageInput
    {
        public object Recipient
        {
            get; set;
        }

        public string Body
        {
            get; set;
        }

    }

Here’s how I’m building my spec context. I’m building a fake form collection and stuffing it into a bindingContext object.

public abstract class given_a_controller_with_valid_email_input : 
            given_a_controller_context
        {
            Establish additional_context = () =>
                               {
                                   var form = new FormCollection
                                                  {
                                                      new NameValueCollection
                                                          {
                                                              { "EmailMessageInput.Recipient", "test@test.com"},
                                                              { "EmailMessageInput.Body", "Test body." }
                                                          }
                                                  };

                                   _bindingContext = new ModelBindingContext
                                                         {
                                                             ModelName = "EmailMessageInput",
                                                             ValueProvider = form
                                                         };

                                   _modelBinder = new DefaultModelBinder();
                               };

            protected static ModelBindingContext _bindingContext;
            protected static DefaultModelBinder _modelBinder;
        }

        public abstract class given_a_controller_context
        {
            protected static MessageController _controller;

            Establish context =
                () =>
                    {
                        _controller = new MessageController();
                    };
        }

Finally, my spec throws an null reference exception when I execute .BindModel() from inside one of my specs:

Because of = () => 
                 {
                     _model = _modelBinder.BindModel(null, _bindingContext);
                 };

Any clue what it could be?

Feel free to ask me for more info, if needed. I might have taken something for granted.

  • 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-14T23:03:57+00:00Added an answer on May 14, 2026 at 11:03 pm

    I’m afraid I’ll have to start with a question: why are you testing the default model binder? The DefaultModelBinder class is part of the framework and so it is not your responsibility to test. You should assume it is a working black box.

    Anyway, looking at your code I think that what you’re trying to accomplish is to get a bound model to test further; am I on the right track? if so, I would recommend you to look into James Broome’s MSpec extensions for MVC

    The source code comes with several examples; but it allows you, for example, to call an action method on a controller and act on the (typed!) ViewModel (the one you pass to the view from the controller action) by simply doing

    It should_display_the_person_name = () => result.Model<Person>().Name.ShouldNotBeNull();
    

    Hope this helps

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.