模拟app.config值 [英] Mock app.config value

查看:91
本文介绍了模拟app.config值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我上课了。

<前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中模拟两个值。如何?

解决方案

你不能模拟一个类,所以你可能需要通过构造函数注入配置并相应地重新设计类及其用法。


Say I have the class.

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.


这篇关于模拟app.config值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆