ASP.NET MVC Beta Released
page 6 of 14
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 58578/ 128

Improved Unit Testing of UpdateModel and TryUpdateModel Scenarios

With Preview 5 you had to use mocking in order to unit test form post scenarios that used the UpdateModel or TryUpdateModel methods.  Today's beta now allows you to unit test all form post scenarios without ever requiring mocking (which enables better friction-free unit testing).

There is a new IValueProvider interface introduced with today's beta that the model binding infrastructure uses to retrieve values to bind (as opposed to always going against the request object).  The FormCollection class (which is built-into the beta) implements this interface - and you can now explicitly pass an instance of this to UpdateModel/TryUpdateModel to bind its values from. 

For example: below in the "Save" action method we are binding all incoming form values to a FormCollection (which will be passed in as an argument to the action method).  I can then pass this form collection to the UpdateModel call and have it map the values onto the person model object using this parameter:

Figure 16

We could then unit test a successful form post scenario for the above action method using the code below (notice how we don't need to mock anything - instead we can just create a formcollection, populate it, and pass it as a parameter):

Figure 17

We could then unit test an unsuccessful form post (which fails because of invalid input for the age value) using the code below.  Notice how we are verifying that the edit form is redisplayed (so that users can correct their problem) in a form-post failure scenario:

Figure 18

We did not have to mock anything to unit test both of the above form submission scenarios.

View Entire Article

User Comments

Title: how about validation   
Name: geocine
Date: 2010-12-15 7:42:28 PM
Values are not mapped but validations are still executed.
Title: sikat ang pinoy   
Name: renanorola
Date: 2010-02-05 8:35:24 PM
Creating silverlight on mvc are now easy. Thanks for the tutorials I like how you have presented the information in full detail. Keep up the great work
Title: ASP.NET MVC   
Name: Ranganathan
Date: 2009-02-09 1:54:22 AM
Today's beta release is a step closer to the final ASP.NET MVC 1.0 product. While not 100% feature complete, we think the major subsystems are all getting really close to being done, and that the quality level is now pretty good.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-23 6:31:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search