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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:42:37+00:00 2026-05-14T04:42:37+00:00

I am working through Steve Sanderson’s book Pro ASP.NET MVC Framework and I having

  • 0

I am working through Steve Sanderson’s book Pro ASP.NET MVC Framework and I having some issues with two unit tests which produce errors.

In the example below it tests the CheckOut ViewResult:

[AcceptVerbs(HttpVerbs.Post)]
public ViewResult CheckOut(Cart cart, FormCollection form)
{
        // Empty carts can't be checked out
        if (cart.Lines.Count == 0)
        {
            ModelState.AddModelError("Cart", "Sorry, your cart is empty!");
            return View();
        }

        // Invoke model binding manually
        if (TryUpdateModel(cart.ShippingDetails, form.ToValueProvider()))
        {
            orderSubmitter.SubmitOrder(cart);
            cart.Clear();
            return View("Completed");
        }
        else // Something was invalid
            return View();
}

with the following unit test

[Test]
public void Submitting_Empty_Shipping_Details_Displays_Default_View_With_Error()
{
    // Arrange
    CartController controller = new CartController(null, null);
    Cart cart = new Cart();
    cart.AddItem(new Product(), 1);
    // Act
    var result = controller.CheckOut(cart, new FormCollection {
        { "Name", "" }                                               
    });
    // Assert
    Assert.IsEmpty(result.ViewName);
    Assert.IsFalse(result.ViewData.ModelState.IsValid);
}

I have resolved any issues surrounding TryUpdateModel by upgrading to ASP.NET MVC 2 (Release Candidate 2) and the website runs as expected.

The associated error messages are:

Tests.CartControllerTests.Submitting_Empty_Shipping_Details_Displays_Default_View_With_Error: 
System.ArgumentNullException : Value cannot be null.
Parameter name: controllerContext

and the more detailed

at System.Web.Mvc.ModelValidator..ctor(ModelMetadata metadata, ControllerContext controllerContext)
at System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, IValueProvider valueProvider)
at WebUI.Controllers.CartController.CheckOut(Cart cart, FormCollection form)

Has anyone run into a similar issue or gotten the test to pass?

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

    You have to mock the HttpContext of the Controller to write such tests. The MvcContrib library makes it quite easy.

    Another option that I prefer is to extract the logic done by “TryUpdateModel” into a custom ModelBinder. You can then test the logic of your checkout action method separately from the logic that update your model.
    No HttpContext needed.

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

Sidebar

Related Questions

I've been working through the book Pro ASP.NET MVC 2 Framework by Steven Sanderson.
I've been working through a fantastic book (Pro ASP.net MVC 2 Framework, Steven Sanderson)
Working through Pro ASP.NET MVC book and I got the following code snippet that
I've been reading Pro ASP.NET MVC Framework, Steven Sanderson, and in chapter 11 it
Just working through a tutorial on asp.net/mvc for the music store application. Ref: http://www.asp.net/mvc/tutorials/mvc-music-store-part-3
I'm working through some MSDN examples, and some books on ADO.Net. What they all
I am working through a KnockOutJs Sample and am having some posting problems with
While working through the MVC 2 NerdDinner tutorial I see that some business logic
In working through the issues with another question, I've found text files with embedded
I'm working through Aaron Hillegass' book, specifically the lottery example. I had a question

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.