Search code examples
asp.net-mvcunit-testingroutesmvccontrib-testhelper

how to test asp.net mvc post actions using MvcContrib.TestHelper


I am trying to test my routing configuration using the excellent MvcContrib.TestHelper and I've run into a problem.

Please assume that my Routing Configuration is set up correctly and initialised in the TestFixture.

I have a controller (TransactionsController) action called Create that takes an input parameter of type TransactionRecord:

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

Currently my test is as follows:

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

My problem is that the Create() method takes a parameter of type TransactionRecord, I do not know how to incorporate this into my test.

I've not been able to find any examples of this nature.


Solution

  • As the TransactionRecord is created from post data, 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)); 
    } 
    

    Answer courtesy of mvccontrib discussion group: http://groups.google.com/group/mvccontrib-discuss/browse_thread/thread/2839edd5ad3c5258