ASP.NET MVC - 单元测试覆盖初始化方法 [英] ASP.NET MVC - Unit Testing Override Initialize Method

查看:231
本文介绍了ASP.NET MVC - 单元测试覆盖初始化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有低于所示得到由所有其他控制器继承了一个抽象类。是否有可能测试在所有这种方法吗?顺便说一句,我试图用最小起订量,但没有运气。如果你能帮助我将更加AP preciated:

 公共抽象类的ApplicationController:控制器
{
    保护覆盖无效初始化(System.Web.Routing.RequestContext的RequestContext)
    {
       base.Initialize(RequestContext的);       //做一些东西在这里
    }
}


解决方案

如果你看看基地的源头code初始化方法,你会发现,它的作用是,它建立了ControllerContext和URL的东西。现在,下载MvcContrib TestHelper,并检查了 TestControllerBuilder 。建设者设置你才能有你依赖于控制器上下文和其他的东西需要的一切。
好吧,我们都还没有结束 - 你想测试自己的初始化的覆盖吗?
TestControllerBuilder犯规打电话给你的初始化,因为它确实在初始化的不同的方式。我建议你​​出分解出您的自定义初始化()逻辑分成不同的方法。然后创建调用此分解出来保护公众初始化方法假(存根)的子类。你和我在一起?

是这样的:

 公共抽象类的ApplicationController:控制器
{   保护覆盖无效初始化(System.Web.Routing.RequestContext的RequestContext)
   {
      base.Initialize(RequestContext的);
      MyInitialzie()
   }
    保护无效MyInitialize()
    {
       ControllerContext.XXX //做whatewer你想在这里。上下文已经设置好的了
    }
}类FakeController:ApplicationController中
{
   公共无效CallMyInitialize()
   {
      MyInitialize();
   }
}

后来在测试类:

  [测试]
公共无效MyInitializeTest()
{
    TestControllerBuilder建设者=新TestControllerBuilder();
    FakeController控制器=新FakeController();
    builder.InitializeController(控制器);    controller.CallMyInitialize();
    // TODO:MyInitialize假设验证
}

清楚了吗?

I've got an abstract class shown below which gets inherited by all the other controllers. Is it possible to test this method at all? Btw, I'm trying to use MOQ but no luck. If you could help me will be much appreciated:

public abstract class ApplicationController : Controller
{  
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
       base.Initialize(requestContext);

       //do some stuff here
    }
}

解决方案

If you take a look at the source code of base Initialize method you will find out that what it does is that it sets up ControllerContext and url stuff. Now, download MvcContrib TestHelper and check out TestControllerBuilder . The builder sets up everything you need in order to have controller context and other stuff which you depend upon. Ok, we are not over yet - you wanted to test your own override of Initialize right? TestControllerBuilder doesnt call your Initialize because it does initialization in different way. I suggest you to factor out your custom Initialize() logic out into different method. Then create fake (stub) subclass with public method that calls this factored out protected Initialize. Are you with me?

something like:

public abstract class ApplicationController : Controller
{  

   protected override void Initialize(System.Web.Routing.RequestContext requestContext)
   {
      base.Initialize(requestContext);
      MyInitialzie()
   }
    protected void MyInitialize()
    {
       ControllerContext.XXX // do whatewer you want here. Context is already setted up
    }
}

class FakeController: ApplicationController 
{
   public void CallMyInitialize()
   {
      MyInitialize();
   }
}

Later in test class:

[Test]
public void MyInitializeTest()
{
    TestControllerBuilder builder = new TestControllerBuilder();
    FakeController controller = new FakeController();
    builder.InitializeController(controller);

    controller.CallMyInitialize();
    //TODO: verification of MyInitialize assumptions
}

Is that clear?

这篇关于ASP.NET MVC - 单元测试覆盖初始化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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