Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Mock a Form Request

I’m just learning ASP.NET MVC and I’m trying to create a mock form request for a unit test.

I’m using RhinoMocks.

I have looked at the following websites but cannot get these to work.

http://blog.maartenballiauw.be/post/2008/03/19/ASPNET-MVC-Testing-issues-Q-and-A.aspx

Update: Controller Code:

    /// <summary>
    /// Creates a new entry
    /// </summary>
    /// <returns></returns>
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([Bind()]Person person) 
    {
        if (Request.Form["DateOfBirth"].ToString() == "")
        {
            TempData["message"] = "Please select a date of Birth";
            ViewData["DateOfBirth"] = Request.Form["DateOfBirth"].ToString();

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, person.ValidationMessages);
            return View();
        }
        else
        { 

        if (person.IsValid())
        {
            person.DateOfBirth = Convert.ToDateTime(Request.Form["DateOfBirth"]);

            personRepository.SaveOrUpdate(person);
            TempData["message"] = person.Firstname + " was successfully added";
            return RedirectToAction("Create", "OrderDetails", new { id = person.ID });
        }
        else
        {

            ViewData["DateOfBirth"] = Request.Form["DateOfBirth"].ToString();

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, person.ValidationMessages);
            return View();
        }

        }

    }
like image 434
Ros Avatar asked Oct 14 '25 16:10

Ros


1 Answers

If you change the action method to have a FormCollection as the final parameter you can then pass in a FormCollection instance that contains all your values. The MVC framework will automatically pass in the values from the form within that parameter when running live.

public ActionResult MyMethod(FormCollection form)
{
    // in testing you will pass in a populated FormCollection object
    // at runtime the framework will populate the form parameter with
    // the contents of the posted form
}

Here is a reasonable example of it being used.

Edit

Have you tried this:

    /// <summary>
    /// Creates a new entry
    /// </summary>
    /// <returns></returns>
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([Bind()]Person person, FormCollection form) 
    {
        if (form["DateOfBirth"].ToString() == "")
        {
            TempData["message"] = "Please select a date of Birth";
            ViewData["DateOfBirth"] = form["DateOfBirth"].ToString();

            MvcValidationAdapter.TransferValidationMessagesTo(
                ViewData.ModelState, person.ValidationMessages);
            return View();
        }
        else
        { 

        if (person.IsValid())
        {
            person.DateOfBirth = Convert.ToDateTime(form["DateOfBirth"]);

            personRepository.SaveOrUpdate(person);
            TempData["message"] = 
                person.Firstname + " was successfully added";
            return RedirectToAction(
                "Create", "OrderDetails", new { id = person.ID });
        }
        else
        {

            ViewData["DateOfBirth"] = form["DateOfBirth"].ToString();

            MvcValidationAdapter.TransferValidationMessagesTo(
                ViewData.ModelState, person.ValidationMessages);
            return View();
        }

        }

    }
like image 80
Garry Shutler Avatar answered Oct 17 '25 11:10

Garry Shutler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!