Click here to Skip to main content
15,747,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

And my BaseController has a definition like this
 public abstract partial class BaseController : Controller
    public BaseController()
     //Code to Base Controller Constructor


So, while unit testing for the controller, is it possible to mock the BaseController, i.e. base class..??..
Please help...
Sergey Alexandrovich Kryukov 1-Mar-13 0:48am    
I'm not getting it. Why mocking? To add test methods to base? or what? Why?
scarletwitch1990 1-Mar-13 0:53am    
no i need to add test methods to the ProductController, to check the controller action., i am able to get thru other dependencies mostly by creating fake repository and fake service layer...but because of the inheritance, i get stuck everytime...
scarletwitch1990 1-Mar-13 0:54am    
any other way instead of mock objects is more preferable....i couldn't find any reference to getting through a inherited class

1 solution

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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900