1

Okay I have a rather difficult issue in this case - i have a class which contains a method that requires a class type of a controller. I have no idea how to mock this test so if anyone can help id really appreciate it!

public void Class(Controller controller, Model model)
    {
        if (model.Address != null && model.Address.Country != null && model.Address.Country.CountryId > 0)
            model.Address.Country = _countryService.Find( *expression for id here* );
        else
            model.Address.Country = null;
        controller.ModelState.RemoveRange(controller.ModelState.Where(i => i.Key.StartsWith("Address.Country")).ToList());
        controller.ModelState.RemoveRange(controller.ModelState.Where(i => i.Key.Contains(" *Stuff here* ")).ToList());
        controller.ModelState.Remove("owner");
    }

so my issue here is that the controller class is an abstract class that inherits from multiple other classes:

public abstract class Controller : Class1, Class2, Class3, Class4, Class5, Class6, Class7, Class8, Class9
{
    *Lots of un-needed code here*
}

i need to be able to mock the controller in order to be able to get my test to run - otherwise i get a no object set reference on the first Controller.ModelState.

any help would be great!

harrison
  • 79
  • 1
  • 11
  • C# [does not support multiple inheritance](http://stackoverflow.com/q/2456154/1698557), so `Controller` can only derive from one other class. Are those other "classes" interfaces? Can you change your class' constructor to use those interfaces instead of `Controller`? – Patrick Quirk Nov 25 '14 at 13:40
  • yes they are interfaces asides one class, and no unfortunately i cant - this is the problem as i cant conclude an optimal or even working/functional way to mock this. – harrison Nov 25 '14 at 13:46

1 Answers1

0

Well after messing around for a while I resorted to using a bit of a well unfavourable work around using Moq's inbuilt method of:

     var _context = new Web.Controllers.Controller(*all needed services etc in here - mocked as privates in test*);

MockContainer.RegisterInstance(_context.GetType(), _context.GetType().FullName, _context, new ContainerControlledLifetimeManager());

so yeah if anyone needs to mock a controller if its that complex with lots of dependencies and methods as mine had, you're probably better of doing this.

harrison
  • 79
  • 1
  • 11