Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing controller action which returns RedirectToRouteResult

I have an action in my controller:

RedirectToRouteResult Create(UserDTO UserDTO)

Which at some point decides with which HTML to respond after a post request by redirecting to an action:

return ModelState.IsValid ? RedirectToAction("ThanksCreate") : RedirectToAction("Register");

In my unit tests I would like to get hold of the ‘views’ modelstate somehow like this:

var modelState = result.ViewData.ModelState;

Assert.IsFalse( modelState.IsValid );

where ‘result’ (ViewResult) is the result of the action ‘Create’ depending on the submitted DTO. My dilemma is that my action ‘returns’ a RedirectToRouteResult which I thought is quite nice but it might not be testable or is it?

How could I get hold of the ModelState in my scenario? Thanks.

Best wishes,

Christian

enter code here
like image 612
cs0815 Avatar asked Feb 06 '26 16:02

cs0815


1 Answers

I updated my answer on your other (related) question. In this scenario, I think it's arguably incorrect to test the model state as it isn't exposed outside the method. Rather, you should use your set up to induce correct/incorrect model state and test that the method has the proper output based on the given set up.

like image 116
tvanfosson Avatar answered Feb 08 '26 04:02

tvanfosson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!