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
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:
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):
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:
We did not have to mock anything to unit test
both of the above form submission scenarios.