且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC - 单元测试覆盖初始化方法

更新时间:2023-02-17 12:07:39

如果你看看基地的源头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?