tags:

views:

254

answers:

2

I use ControllerActionInvoker to invoke controller actions form unit tests

var controllerInvoker = new ControllerActionInvoker();
var result = controllerInvoker.InvokeAction(
                 testController.ControllerContext, "Default" );

How do i use it to call an action that has paramters?

[AcceptVerbs( HttpVerbs.Post )]
[ActionException( SomeAttribute )]
public SomeResult AddMethod( long[] Ids )
{
    //some code
}
A: 

From the documentation it looks like you want to use the InvokeActionMethod method which allows you to pass parameters in an IDictionary as the third argument.

The ControllerContext actually carries with it additional data that the controller will use for binding (filters, model binders, route data). Your argument will need to be passed through the ControllerContext.

I found an example about unit testing controllers.

cfeduke
IT is a protected method.
Good point, let me update after some research...
cfeduke
A: 

You shouldn't use the ControllerActionInvoker from within your unit tests. What are you actually trying to accomplish?

If you're trying to test the behavior of your actions, just call them directly (they are just regular methods). If you're trying to test the behavior of your filters, create a mock context for the filter and call its OnXxx() method.

Levi