4

I'm using restful routing module for asp.net mvc and very happy with it. But I can't get one thing. For example I had a controller action like this:

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

And had no problem with writing a spec 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 with use of rest, I want to have an Index method like this:

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

Sure that previous spec fails, because action result is not of type ViewResult, its of type FormatResult. FormatResult by itself overrides ExecuteResult method that returns void. How can I unit test such case if I want to verify action result types and data inside FormatResult?

3 Answers 3

1

In the future version of restful routing such code is possible:

var formatResult = actionResult as FormatResult;
ActionResult result = formatResult.ExposeResult().Html();
result.ShouldBeOfType<ViewResult>();
Sign up to request clarification or add additional context in comments.

Comments

0

Could assert use the name of the view returned?

That wouldn't give you the format but would let you test the view returned.

1 Comment

I can't get instance of ViewResult
0

It depends on the request to which ActionResult gets used. This logic takes place when the FormatResult's ExecuteResult method is run. The best way around this would be to refactor the FormatResult class so that you can get the chosen ActionResult without executing it. Pull requests welcome :)

As it stands the only way to test this would be to run the ExecuteResult method and inspect the outcome.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.