How to test http post routes with parameters

65 views
Skip to first unread message

Paul Wallace

unread,
Feb 1, 2010, 12:33:04 PM2/1/10
to mvccontrib-discuss
Hi,
I have a controller action:

[Trace, AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(TransactionRecord tx)
{
...
}

Assume that my routing is configured correctly. I want to test the
routing using MvcContrib.TestHelper, I have the following test:

[Test]
public void TestRoute_POST_Transactions_Create()
{
"~/Transactions/".WithMethod
(HttpVerbs.Post).ShouldMapTo<Web.Controllers.TransactionsController>(x
=> x.Create(???));
}

How do I incorporate an instance of TransactionRecord into the test?
What do I put in place of ??? to be able to run the test?

Perhaps I'm not taking the right approach, any guidance as to how to
correctly test this screnario is welcome.

BTW, I also posted this on Stack Overflow:
http://stackoverflow.com/questions/2176617/how-to-test-asp-net-mvc-post-actions-using-mvccontrib-testhelper

Cheers

Paul

Jeremy Skinner

unread,
Feb 1, 2010, 4:10:08 PM2/1/10
to mvccontri...@googlegroups.com
Hi Paul,

Assuming that the TransactionRecord is created from post data, then you'd just pass null to your test:

[Test]
public void TestRoute_POST_Transactions_Create()
{
   "~/Transactions/".WithMethod(HttpVerbs.Post).ShouldMapTo<Web.Controllers.TransactionsController>(x => x.Create(null));
}

When using the route tester, the parameters you pass to your actions are only relevant if they're coming from route values. In the case of a form post then this is not the case, so you can safely pass null.

Jeremy


--
Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go to http://mvccontrib.org

To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

Paul Wallace

unread,
Feb 2, 2010, 8:41:42 AM2/2/10
to mvccontrib-discuss
Hi Jeremy,

Thanks, that sorted the problem. And thanks for the explanation as
well, makes sense.

Cheers

Paul

On Feb 1, 9:10 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Hi Paul,
>
> Assuming that the TransactionRecord is created from post data, then you'd
> just pass null to your test:
>
> [Test]
> public void TestRoute_POST_Transactions_Create()
> {
>

> "~/Transactions/".WithMethod(HttpVerbs.Post).ShouldMapTo<Web.Controllers.Tr ansactionsController>(x


> => x.Create(null));
>
> }
>
> When using the route tester, the parameters you pass to your actions are
> only relevant if they're coming from route values. In the case of a form
> post then this is not the case, so you can safely pass null.
>
> Jeremy
>

> On 1 February 2010 17:33, Paul Wallace <p...@questern.com> wrote:
>
>
>
> > Hi,
> > I have a controller action:
>
> >  [Trace, AcceptVerbs(HttpVerbs.Post)]
> >    public ActionResult Create(TransactionRecord tx)
> >    {
> >        ...
> >    }
>
> > Assume that my routing is configured correctly.  I want to test the
> > routing using MvcContrib.TestHelper, I have the following test:
>
> > [Test]
> > public void TestRoute_POST_Transactions_Create()
> > {
> >    "~/Transactions/".WithMethod
> > (HttpVerbs.Post).ShouldMapTo<Web.Controllers.TransactionsController>(x
> > => x.Create(???));
> > }
>
> > How do I incorporate an instance of TransactionRecord into the test?
> > What do I put in place of ??? to  be able to run the test?
>
> > Perhaps I'm not taking the right approach, any guidance as to how to
> > correctly test this screnario is welcome.
>
> > BTW, I also posted this on Stack Overflow:
>

> >http://stackoverflow.com/questions/2176617/how-to-test-asp-net-mvc-po...


>
> > Cheers
>
> > Paul
>
> > --
> > Contact Jeffrey Palermo or Eric Hexter with specific questions about the

> > MvcContrib project.  Or go tohttp://mvccontrib.org

Reply all
Reply to author
Forward
0 new messages