I have a QuickController class that inherits from a BaseController. A method in QuickController calls a property on the BaseController which have a dependency on ConfigurationManager.AppSettings.
I want to unit test QuickController but can't find a way to get rid of that dependency. Here's my test:
[TestMethod]
public void TestMethod1()
{
var moqServiceWrapper = new Mock<IServiceWrapper>();
var controller = new QuickController(moqServiceWrapper.Object);
//Act
var result = controller.Estimator(QuickEstimatorViewModel);
//Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
}
The QuickController class
public class QuickController : BaseController
{
public QuickController(IServiceWrapper service)
: base(service) { }
public ActionResult Estimator(QuickEstimatorViewModel viewModel)
{
viewModel.RiskAddressLocation = RiskAddressLocation;
....
return View("QuickQuote", viewModel);
}
}
And the BaseController property
public RiskAddressLocation RiskAddressLocation
{
get { return ConfigurationManager.AppSettings["..."]
.ToEnum<RiskAddressLocation>(true); }
}
I also tried to call the method on a FakeQuickController that inherit from QuickController, but can't override the property, it's the one in the BaseController that is always called.
Is there anything I can do here?
Update
From the accepted answer here's what I had that VS2013 didn't like
public class BaseController{
public virtual RiskAddressLocation RiskAddressLocation {get{...;}
}
public class QuickController : BaseController{}
public class FakeQuickController : QuickController{
public override RiskAddressLocation RiskAddressLocation
{
get { return ...} // Doesn't compile (cannot override because
//BaseController.RiskAddressLocation' is not a function
}
}
This however, works fine
public class BaseController{
public virtual RiskAddressLocation RiskAddressLocation(){...}
}
public class QuickController : BaseController{}
public class FakeQuickController : QuickController{
public override RiskAddressLocation RiskAddressLocation()
{
return ... ;
}
}