-1

I'm sending some param via form post from my template and the action in my controller is handling it accordingly.

Now I want to write a test case for that action. How should I make that dummy request so that Request.Form["selectedSuppliersHidden"] will work in controller action?

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

You shouldn't really need to use Request.Form in MVC

Give this a read: http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx

If you handle posting correctly then you'll have an Action that can accept variables and be tested a lot easier.

EDIT You can use the FormCollection param in your Action, something like this

[HttpPost]
public ActionResult Index(string btnSubmit, FormCollection collection)
{
    //btnSubmit this is the button that is clicked.
    return View();
}

The FormCollection will have everything in it from the Request.Form collection. But you should still be able to post the right hand listbox in the normal MVC way

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
  • I had a special case for what I had to make that Request.Form ... but now I need to test it also – Prasenjit Paul Mar 11 '14 at 09:20
  • You'll need to highlight what that special case is and maybe include some code. Also you'd need to fake the Request object in your test methods, not sure how you'd do that. – matt_lethargic Mar 11 '14 at 09:21
  • I want a dropdown like this [link](http://www.voidcanvas.com/demo/7511pair-select/) .... And I have to post everything in the right hand side dropdown – Prasenjit Paul Mar 11 '14 at 09:24
  • Thank you very much. This solves my Request.Form problem. But Here is another one.... Now from my test method, even if I send null value to some parameters of my model (those parameters had [Required] attribute), it says ModelState.IsValid = true – Prasenjit Paul Mar 11 '14 at 11:03
  • But it should have been false – Prasenjit Paul Mar 11 '14 at 11:03
  • If this solves your problem, can you accept my answer. Your other problem sounds like a separate issue and in which case you'll need to start a new question and also provide some code, cannot give a proper solution without it. Thanks – matt_lethargic Mar 11 '14 at 11:59
  • Actually the thing was controller doesn't deal with model validation. Hence the problem I mentioned is not a part controller unit test. So my all problems are solved :) – Prasenjit Paul Mar 11 '14 at 12:35