如何对使用静态类的方法进行单元测试,而该静态类又使用 ConfigurationElementCollection?

2024-02-09

public class ConfigSection : ConfigurationSection
{        
    public static ConfigSection GetConfigSection()
    {
        return (ConfigSection)System.Configuration.ConfigurationManager.
           GetSection("ConfigSections");
    }

    [System.Configuration.ConfigurationProperty("ConstantsSettings")]
    public ConstantSettingCollection ConstantsSettings
    {
        get
        {
            return (ConstantSettingCollection)this["ConstantsSettings"] ??
               new ConstantSettingCollection();
        }
    }


    public class ConstantSettingCollection : ConfigurationElementCollection
    {   

        public ConstantElements this[object key]
        {
            get
            {
                return base.BaseGet(key) as ConstantElements;
            }
            set
            {
                if (base.BaseGet(key) != null)
                {
                    base.BaseRemove(key);
                }
                this.BaseAdd(this);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ConstantElements();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ConstantElements)element).Key;
        }
    }

    public class ConstantElements : ConfigurationElement
    {
        [ConfigurationProperty("key", IsRequired = true)]
        public string Key
        {
            get
            {
                return this["key"] as string;
            }
        }

        [ConfigurationProperty("val", IsRequired = true)]
        public string Constants
        {
            get { return this["value"] as string; }
        }          
    }        
}
    
public class ConstantHelper
{   
    public static string ConstantForLog
    {
        get
        {
            return ConfigSection.GetConfigSection().ConstantsSettings["ConstantForLog"].Constants;
        }
    }
}

上面的单元测试的全新内容是从应用程序配置中读取一些常量值的代码 这是我在构造函数中的代码已分配的值。

public class HomeController
{
    protected string constants;
    public HomeController()
     {
         constants = ConstantHelper.ConstantForLog;
     }
 }

测试代码

[TestClass]
public class HomeControllerTester
{
 [TestMethod]
 public void Initialize_Tester()
  {
    //Creating Instance for the HomeController
    HomeController controller = new HomeController();
  }
}

调试时发现ConstantHelper类不读取Appsettings

Solution

找到了解决方案,实际上它工作得很好,错误是在 app.config 中完成的

我遇到的另一个问题是在 ConfigSection 中 对于 MVC 应用程序 web.config 不需要命名空间type="type"至于单元测试 app.config 需要命名空间type="type,_命名空间”


你必须注射ConstantHelper into HomeController。您可以将其连接出来然后注入。从单元测试传递一个模拟对象IConstantHelper.

UPDATE

我为 ConstantHelper 类定义了一个接口,让我有机会注入和模拟依赖项。

ConstantHelper.cs

public class ConstantHelper : IConstantHelper
{
    public string ConstantForLog
    {
        get
        {
            return ConfigSection.GetConfigSection().ConstantsSettings["ConstantForLog"].Constants;
        }
    }
}

public interface IConstantHelper
{
    string ConstantForLog { get; }
}

HomeController.cs

请注意,我现在从外部注入常量助手,以便单元测试可以模拟它。

public class HomeController : Controller
{
    private readonly IConstantHelper _constantHelper;

    public HomeController(IConstantHelper constantHelper)
    {
        _constantHelper = constantHelper;
    }

    public ActionResult Index()
    {
        return View(_constantHelper.ConstantForLog);
    }
}

HomeControllerTest.cs

[TestClass]
public class HomeControllerTest
{
    [TestMethod]
    public void Index_WithDependecySetupCorrectly_ReturnTestString()
    {
        var mockHelper = new Mock<IConstantHelper>();
        const string testDataString = "TestString";
        mockHelper.Setup(z => z.ConstantForLog).Returns(testDataString);

        //Creating Instance for the HomeController
        var controller = new HomeController(mockHelper.Object);

        var result = controller.Index() as ViewResult;

        Assert.IsNotNull(result);
        Assert.AreEqual(testDataString, result.ViewName);
    }
}

我使用 Moq 模拟框架。只需在测试项目中的包管理器控制台中使用以下命令安装它:

安装包起订量

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何对使用静态类的方法进行单元测试,而该静态类又使用 ConfigurationElementCollection? 的相关文章

随机推荐