How can I test an event of a MVC controller

陌路散爱 提交于 2019-12-21 04:42:40

问题


I want to test the OnException, OnActionExecuted event of an MVC controller.

If I use mock like this:

        var httpContext = MockRepository.GenerateMock<HttpContextBase>();
        var request = MockRepository.GenerateMock<HttpRequestBase>();

        httpContext.Expect(c => c.Request).Return(request).Repeat.AtLeastOnce();
        request.Expect(r => r.IsAuthenticated ).Return(true).Repeat.AtLeastOnce();


        var controller = new MyController() ;

        controller.ControllerContext = new ControllerContext(httpContext,
                                                             new RouteData(),
                                                             controller);

        var result = controller.Execute() as ViewResult;

…the action method is executing, but the events are not invoked.


回答1:


This is one of the separation of concerns principles of MVC. When you're unit testing a method, you're testing the method itself independent of any filters applied to it. (And OnException() and OnActionExecuting() are really just glorified filters.)

If you want to test those other methods independently, you're free to do so. Normally the way you'd go about this is by calling the filters like so:

((IActionFilter)controller).OnActionExecuting(...)
((IExceptionFilter)controller).OnException(...)

You'll have to create context objects to pass to these methods. In the end, you have three unit tests: one for OnActionExecuting(), one for OnException(), and one for the actual method you're testing. The nice thing about this setup is that once you've unit tested the filters once, you don't have to worry about them any more for any other unit tests.

For example, if you have a Method1(), Method2(), and Method3(), you don't need to test each combination of method + filters. Simply have five unit tests: OnActionExecuting(), OnException(), Method1(), Method2(), and Method3(). This eliminates redundant testing and makes it easier to track down potential bugs in your code.



来源:https://stackoverflow.com/questions/1678927/how-can-i-test-an-event-of-a-mvc-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!