我有一个从BaseController继承的QuickController类。 QuickController中的一个方法调用对ConfigurationManager.AppSettings具有依赖性的BaseController上的属性。测试依赖于基本属性的方法
我想单元测试QuickController,但无法找到摆脱这种依赖关系的方法。下面是我的测试:
[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));
}
的QuickController类
public class QuickController : BaseController
{
public QuickController(IServiceWrapper service)
: base(service) { }
public ActionResult Estimator(QuickEstimatorViewModel viewModel)
{
viewModel.RiskAddressLocation = RiskAddressLocation;
....
return View("QuickQuote", viewModel);
}
}
而且BaseController财产
public RiskAddressLocation RiskAddressLocation
{
get { return ConfigurationManager.AppSettings["..."]
.ToEnum<RiskAddressLocation>(true); }
}
我还试图呼吁,从QuickController继承FakeQuickController的方法,但可以”不会覆盖属性,而是始终调用BaseController中的属性。
我能在这里做什么吗?
更新
从这里接受的答案是什么,我有这样的VS2013不喜欢
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
}
}
然而,这工作得很好
public class BaseController{
public virtual RiskAddressLocation RiskAddressLocation(){...}
}
public class QuickController : BaseController{}
public class FakeQuickController : QuickController{
public override RiskAddressLocation RiskAddressLocation()
{
return ... ;
}
}
好像你想[mock ConfigurationManager](http://stackoverflow.com/questions/9486087/how-to-mock-configurationmanager-appsettings-with-moq)而不是尝试嘲笑基地? – CodingIntrigue 2014-09-25 12:14:25
@RGraham够了,但是如何拦截基类中的RiskAddressLocation以便能够模拟它呢? – kooshka 2014-09-25 13:23:12