Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: UnitTesting MVC4
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...
Posted 28-Feb-13 19:44pm
Comments
Sergey Alexandrovich Kryukov at 1-Mar-13 0:48am
   
I'm not getting it. Why mocking? To add test methods to base? or what? Why?
—SA
sarahalex@22 at 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...
sarahalex@22 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100