开发者

testing controller action which returns RedirectToRouteResult

开发者 https://www.devze.com 2022-12-30 16:59 出处:网络
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:

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.开发者_Python百科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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消