Personally I Use MVCContrib TestHelper to unit test my controller actions. It makes things very fun and easy.
So in your case assuming the following controller (disclaimer: absolutely never write something like this in a real application, it's just an example here, in a real world application controller actions should never fetch stuff from Request.Form
, they should use strongly typed action parameters and leave the default model binder do the parsing, etc...):
public class MyViewModel
{
public string SomeProperty { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
var json = Request.Form[0];
var model = new JavaScriptSerializer().Deserialize<MyViewModel>(json);
return View(model);
}
}
you could test it like this:
// arrange
var builder = new TestControllerBuilder();
var sut = new HomeController();
builder.InitializeController(sut);
builder.Form.Add("foo", "{ someProperty: 'some value' }");
// act
var actual = sut.Index();
// assert
actual
.AssertViewRendered()
.WithViewData<MyViewModel>()
.SomeProperty
.ShouldEqual("some value", "");