Click here to Skip to main content
11,928,987 members (54,437 online)
Rate this:
Please Sign up or sign in to vote.
See more: MVC4 unit-testing
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
I'm not getting it. Why mocking? To add test methods to base? or what? Why?
sarahalex@22 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 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
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 1 Mar 2013
Copyright © CodeProject, 1999-2015
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