How can I unit test HandleUnknownAction () of an ASP.NET MVC controller?

I have the following set of HandleUnknownAction in my base controller:

protected override void HandleUnknownAction(string action)
{
    Response.Redirect("/");
}

How can I unit test what? Another point: is it right to handle an unknown action? It seems that calling RedirectToAction () would be more correct, but HandleUnknownAction has no return value.

Yes, I can check:

[Test]
public void TestHandleUnknownAction()
{
    ctroler.ActionInvoker.InvokeAction(ctroler.ControllerContext, "unknown");
}

I am stuck in this.

+3
source share
2 answers

, , HandleUnknownAction , . , . , , HandleUnknownAction Moq. Rhino Mocks.

public void TestHandleUnknownAction()
{
    Mock<ControllerContext> cc = new Mock<ControllerContext>
                                           (MockBehavior.Strict);
    cc.Expect(c => c.HttpContext.Response.Redirect("/"));

    TestHelperController controller = new TestHelperController();
    controller.ControllerContext = cc.Object;

    controller.InvokeUnknownAction("test");
}

TestHelperController HandleUnknownAction:

public class TestHelperController : RealController
{
    public void InvokeUnknownAction(string action)
    {
        this.HandleUnknownAction(action);
    }
}
+3

Response.Redirect, unit test , - , :

// TODO - Put some stuff into ViewData or a model
View("Error").ExecuteResult(Me.ControllerContext)
+1

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


All Articles