我有一個從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