我该如何进行单元测试我的asp.net-MVC控制器的OnActionExecuting的方法? [英] How do I unit test my asp.net-mvc controller's OnActionExecuting method?

查看:382
本文介绍了我该如何进行单元测试我的asp.net-MVC控制器的OnActionExecuting的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重写我的控制器的OnActionExecuting方法设置基础上,执行filterContext一些内部状态。如何测试呢?该方法本身是受保护的,所以我认为我得走了较高的调用栈中。

I've overridden my controller's OnActionExecuting method to set some internal state based on the executing filterContext. How do I test this? The method itself is protected so I assume I'll have to go higher up in the call stack.

什么code,我需要对此进行测试?

What code do I need to test this?

我使用MVC RC 1。

I'm using mvc RC 1.

编辑:我还使用NUnit的

I'm also using nunit.

感谢

推荐答案

您需要添加并使用专用访问器。在你的控制器类右键单击并选择从菜单中创建私有访问器,并将它们添加到您的测试项目。一旦在测试项目中,创建你的控制器,然后为它创建一个访问。该方法应该是可在存取器。下面是从我自己的code进行样品测试:

You need to add and use a Private Accessor. Right click in your controller class and choose Create Private Accessors from the menu and add them to your test project. Once in your test project, create your controller, then create an accessor for it. The method should be available on the accessor. Here's a sample test from my own code:

/// <summary>
///A test for OnActionExecuting
///</summary>
[TestMethod()]
[ExpectedException( typeof( InvalidOperationException ) )]
public void OnActionExecutingWindowsIdentityTest()
{
    var identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal( identity );
    var httpContext = MockRepository.GenerateStub<HttpContextBase>();
    httpContext.User = principal;

    var actionDescriptor = MockRepository.GenerateStub<ActionDescriptor>();

    RouteData routeData = new RouteData();

    BaseController controller = new BaseController();
    BaseController_Accessor accessor = new BaseController_Accessor( new PrivateObject( controller ) );
    ControllerContext controllerContext = MockRepository.GenerateStub<ControllerContext>( httpContext, routeData, controller );

    ActionExecutingContext filterContext = new ActionExecutingContext( controllerContext, actionDescriptor, new Dictionary<string, object>() );

    accessor.OnActionExecuting( filterContext );

}

修改:如果你不使用MSTest的为你的单元测试,您可能需要手动生成的访问。从本质上讲,你让一个公开下通过等效的公共方法测试类的私有/保护方法的封装类,测试通过类的实例来包装,然后利用反射从包装类调用私有/保护方法对被测类。

EDIT: If you aren't using MSTest for your unit tests, you may have to generate the accessors by hand. Essentially, you make a wrapper class that exposes the private/protected methods of the class under test via equivalent public methods, pass an instance of the class under test to the wrapper, and then use reflection from the wrapper class to invoke the private/protected method on the class under test.

   public class MyClass
   {
       protected void DoSomething( int num )
       {
       }
   }

   public class MyClass_accessor
   {
       private MyClass privateObj;

       public MyClass_accessor( MyClass obj )
       {
           this.privateObj = obj;
       }

       public void DoSomething( int num )
       {
           MethodInfo info = privateObj.GetType()
                                       .GetMethod("DoSomething",
                                                   BindingFlags.NonPublic
                                                   | BindingFlags.Instance );

           info.Invoke(obj,new object[] { num });
       }
    }

这篇关于我该如何进行单元测试我的asp.net-MVC控制器的OnActionExecuting的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆