With ASP.NET/MVC + MvcMockHelpers + RhinoMocks, which sets RouteData?

I am trying to write a test for an ASP.NET MVC controller method. I have RhinoMocks (since it seems to be the most popular and best), and MvcMockHelpers (since I think I need what).

My validation method looks like this:

var controller = new MyController();
MvcMockHelpers.SetFakeControllerContext(mocks, controller);
mocks.ReplayAll();
var result = controller.MyAction(arg1, arg2);
// then i'd make assertions on the result

      

This doesn't work because the RouteData parameter has no entries, and MyAction depends on having a set of values ​​such as "controller" and "action".

I know I can create a custom RouteData for every controller method I want to call, but that seems silly. What part of ASP.NET MVC is responsible for setting this up and how can I get into this code path?

(I feel completely lost here, so if I bark on the wrong tree, let me know that too.)

+2


source to share


3 answers


I found better luck with the MvcContrib Test Helper which uses RhinoMocks.

This is a worthy introduction



And the MVC Contrib project is on CodePlex

+1


source


I suggest you split device testing into routing tests and controller tests. Routing tests will test routes regardless of controllers. This allows you to test your controllers in a traditional way like any other Plain Old CSharp method.

Phil Haack and Adam Tibor have good articles on how to check your routes:



Testing Routes in ASP.NET MVC
http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx

Testing Routes Using Rhino Mocks
http://abombss.com/blog/2007/12/10/ms-mvc-testing-routes/

0


source


You may be interested in the way to create and test CodeCampServer routes.

RouteConfigurator: (note that it cleans up routes every time RegisterRoutes () is called - required for the MVCContrib route validation helper) http://code.google.com/p/codecampserver/source/browse/trunk/src/UI/ RouteConfigurator.cs

IncomingRouteTester: Uses the MVCContrib route testing extensions to test routes efficiently: http://code.google.com/p/codecampserver/source/browse/trunk/src/UnitTests/UI/Routes/IncomingRouteTester.cs

Viewing the source of this project (among other things) answered many questions for me, and I would recommend that you download the source and look around the unit tests project if nothing else.

0


source







All Articles