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 parameters?
[AcceptVerbs( HttpVerbs.Post )]
[ActionException( SomeAttribute )]
public SomeResult AddMethod( long[] Ids )
{
//some code
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With