Unit testing asp.net mvc restful routing FormatResult

I use the calm routing module for asp.net mvc and am very happy with it. But I can’t get one. For example, I had a controller action as follows:

public ActionResult Index() { if (Request.IsAjaxRequest()) return PartialView(); return View(); } 

And there was no problem writing a specification like this:

 [Subject(typeof(LotsController))] public class When_Index_called { static LotsController controller; static ActionResult actionResult; Establish context = () => { controller = mocker.Create<LotsController>(); controller.ControllerContext = Contexts.Controller.Default; }; Because of = () => actionResult = controller.Index(); It should_render_view = () => actionResult.AssertViewRendered().ForViewWithDefaultName(); 

But using rest, I want to have an Index method as follows:

 public ActionResult Index() { return RespondTo(format => { format.Html = () => { if (Request.IsAjaxRequest()) return PartialView(); return View(); }; format.Json = () => Json(new { }); }); } 

I am sure that the previous spec does not work, because the result of the action is not of type ViewResult, its type is FormatResult. FormatResult itself overrides the ExecuteResult method, which returns void. How can I unit test such a case if I want to check the result types and result data in FormatResult?

+4
source share
3 answers

In a future version of quiet routing, such code is possible:

 var formatResult = actionResult as FormatResult; ActionResult result = formatResult.ExposeResult().Html(); result.ShouldBeOfType<ViewResult>(); 
+1
source

Can I use the name of the returned view?

This will not give you the format, but allow you to check the returned view.

0
source

It depends on the request to which ActionResult is used. This logic is executed when the FormatResult ExecuteResult method is run. It would be best to reorganize the FormatResult class so that you can get the selected ActionResult without executing it. Invitations on request are welcome :)

As the only way to verify this, run the ExecuteResult method and verify the result.

0
source

Source: https://habr.com/ru/post/1386681/


All Articles