【问题标题】:How to unit test a method which uses a static class which in turn uses ConfigurationElementCollection?如何对使用静态类的方法进行单元测试,该静态类又使用 ConfigurationElementCollection?
【发布时间】:2014-10-29 07:04:03
【问题描述】:
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();
  }
}

在调试时发现 Appsettings 不被 ConstantHelper 类读取

解决方案

发现解决方案实际上它工作正常错误是在 app.config 中完成的

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

【问题讨论】:

  • 已将 app.config 文件添加到测试项目中

标签: c# asp.net-mvc unit-testing


【解决方案1】:

您必须将ConstantHelper 注入HomeController。您可以将其接口出来,然后注入它。从单元测试通过IConstantHelper 的模拟对象。

更新

我为 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 模拟框架。只需在测试项目的包管理器控制台中使用以下命令安装它:

安装包最小起订量

希望这会有所帮助。

【讨论】:

  • 你能用示例解释一下吗
  • 感谢您的解决方案,这也对我有所帮助,花了两天时间才掌握了基本知识,它解决了我的问题。以上虚拟会员有一些问题
  • @Krishjs - 如果这解决了您的问题,您能否将其标记为答案。谢谢。
猜你喜欢
  • 2017-02-16
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
相关资源
最近更新 更多