views:

50

answers:

1

Hi

I have a base controller that I made so I can pass data to the master page view easily. However this base controller gets a service layer passed into it and everything time I run my unit tests this service layer kills it since it tries to access some database stuff.

  private ServiceLayer service;

        public ApplicationController():this(new ServiceLayer())
        {
        }

        public PlannerApplicationController(IServiceLayer serviceS)
        {
            service= serviceS;           
        }

        protected override void Initialize(RequestContext requestContext)
        {

            base.Initialize(requestContext);
          // some stuff gets called here.
        }

First thing the service layer calls

   public ServiceLayer ()
            : this(new Repository())
        {

        }

// have another constructor for DI.

So when I run my tests and it goes to my controller that inherits this base controller once it hits my controllers constructor it seems to call this base controller.

So in my unit tests I tried to mock up the base controller by doing something like this

baseController = new ApplicationController(SerivceLayerInterface);

I using moq and stuff to mock up the repository in the serviceLayer interface but it seems to have no effect.

So not sure what to do.

+3  A: 

Instead of mocking up your base controller, why don't you mock up the service layer interface. For example, using MoQ you can do this:

var serviceMock = new Mock<IServiceLayer>();
//serviceMock.Setup(s => s.SomeMethodCall()).Returns(someObject);
var controller = new BaseController(serviceMock.Object);

The general idea is if you're testing your controller, you want to mock its dependencies. You want to avoid mocking the very thing you are testing.

Haacked
hmm. See this is what I been wondering should I be testing the service layer separately? Or in conjunction with the controller layer? The reason why I say this is lots of my service layer stuff has like validation methods in it. So I thought might as well send the validation data through the view and then test what is in the model state at the end. Since like most of the view stuff is just passing values to the service layer then having a couple if statements to check if the validation was successful and return the right view according to the result from the service layer.
chobo2
Also what I been finding is I have to do alot of mocking up. Someone told me once that if your mocking up too much your doing something wrong. Yet I just don't see how it would be possible without mocking it up. Like almost every method calls something from the database, Or uses some built in asp.net membership thing like(User.Identity...) that seems to die if you don't mock it up. So I just don't see how that can be the case.
chobo2