ASP.NET MVC - format form request

I am just learning ASP.NET MVC and Im trying to create a layout form request for unit test.

Im using RhinoMocks.

I have looked at the following websites, but cannot make them 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();
        }

        }

    }
+3
source share
3 answers

If you change the action method to have FormCollectionas the last parameter, you can pass an instance FormCollectioncontaining all of your values. The MVC structure will automatically pass values ​​from the form inside this parameter when live starts.

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
}

.

Edit

:

    /// <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();
        }

        }

    }
+2

, http://mvccontrib.codeplex.com/:

var form = new NameValueCollection(); form.Add( "publish", "true" ); _controller.Request.Stub(x = > x.Form).IgnoreArguments(). ();

+2

MVC, , , ?

, , :

controller.ActionInvoker.InvokeAction(ctx);

ctx ControllerContext, .. ( MoQ).

+1

Source: https://habr.com/ru/post/1704818/


All Articles