如何模拟继承的基类 [英] How to mock inherited base class

查看:114
本文介绍了如何模拟继承的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的控制器类继承自basecontroller而不是控制器,如下所示:

Say my controller class is inheriting from a basecontroller instead of controller like shown below:

public partial class ProductController : BaseController
   {
         public ActionResult Index()
         {
          //Code to the controller action
          }
   }



我的BaseController有这样的定义


And my BaseController has a definition like this

 public abstract partial class BaseController : Controller
{
    public BaseController()
    {
     //Code to Base Controller Constructor
    }

}



因此,在对控制器进行单元测试时,是吗可以模拟BaseController,即基类.. ?? ..

请帮助...


So, while unit testing for the controller, is it possible to mock the BaseController, i.e. base class..??..
Please help...

推荐答案

如果你使用RahnoMock框架对于Mocking来说,这是可能的。您可以访问链接

但我的个人观点是摘要应该总是抽象的。在您的自定义基本控制器中是一个抽象类。所以它不应该直接测试。为什么因为它是抽象的,所以不可能创建对象。您可以做什么,您可以创建该基本控制器的子类,仅用于单元测试(子类可能存在于您的单元测试项目中),并且可以执行任何操作并使用此所有测试位置。但是,虽然Mocking Framework提供了这个功能,但其他人的意见可能就在那里。我只说个人意见。
If you use RahnoMock framework for Mocking then it is possible. You can visit the Link.
But my personal opinion is Abstract should be always abstract. In your custom base controller is an abstract class. So it should not be testing directly. Why because it is an abstract so no object creation is possible. What you can do you can create a subclass of that base controller for only unit test(subclass might be exists to your unit test project) and can follow do nothing trick and use this all your test places. But again though Mocking Framework provides the feature so others opinion might be there. I say only my personal opinion.


这篇关于如何模拟继承的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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