模拟app.config值 [英] Mock app.config value
问题描述
说我上课了。 Say I have the class. 这篇关于模拟app.config值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<前lang =c#> 公开 class FundsTransferAuthorization
{
public decimal MaxAmount {获得跨度>; set ; }
public decimal MinAmount { get 跨度>; set ; }
private readonly IPromptPlayer _promptPlayer;
public FundsTransferAuthorization(IPromptPlayer promptPlayer)
{
_promptPlayer = promptPlayer;
GetAuthorizationParameters();
}
private void GetAuthorizationParameters()
{
_maxDigit = _promptPlayer.MaxDigit;
MaxAmount = Convert.ToDecimal(GetMaximumAmount());
MinAmount = Convert.ToDecimal(GetMinimumAmount());
}
public virtual string GetMinimumAmount()
{
return ConfigurationManager.AppSettings [ minimumAmount];
}
public virtual string GetMaximumAmount()
{
return ConfigurationManager.AppSettings [ maximumAmount];
}
我想用moq在app.config中模拟两个值。如何?你不能模拟一个类,所以你可能需要通过构造函数注入配置并相应地重新设计类及其用法。
public class FundsTransferAuthorization
{
public decimal MaxAmount { get; set; }
public decimal MinAmount { get; set; }
private readonly IPromptPlayer _promptPlayer;
public FundsTransferAuthorization(IPromptPlayer promptPlayer)
{
_promptPlayer = promptPlayer;
GetAuthorizationParameters();
}
private void GetAuthorizationParameters()
{
_maxDigit = _promptPlayer.MaxDigit;
MaxAmount = Convert.ToDecimal(GetMaximumAmount());
MinAmount = Convert.ToDecimal(GetMinimumAmount());
}
public virtual string GetMinimumAmount()
{
return ConfigurationManager.AppSettings["minimumAmount"];
}
public virtual string GetMaximumAmount()
{
return ConfigurationManager.AppSettings["maximumAmount"];
}
I want to mock up two values in app.config with Moq. How?You cannot Mock a class so you may need to inject the configuration via constructor and redesign the class and its usage accordingly.