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

时间:2014-10-29 07:04:03

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

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;
        }
    }
}

以上单元测试的新功能是从app config读取一些常量值的代码 这里是我在构造函数中的代码已赋值。

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

解决方案

找到解决方案实际上它工作正常错误在app.config

完成

我遇到的另一个问题是ConfigSection 对于MVC app web.config,不需要命名空间 type =" type " 对于单元测试app.config,需要命名空间 type =" type ,_ namespace"

1 个答案:

答案 0 :(得分:4)

您必须将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模拟框架。只需在测试项目的Package Manager Console中使用以下命令安装它:

  

Install-Package Moq

希望这有帮助。