【问题标题】:Unit Testing custom ConfigurationElement & ConfigurationElementCollection单元测试自定义 ConfigurationElement 和 ConfigurationElementCollection
【发布时间】:2014-10-08 03:29:06
【问题描述】:

我创建了自定义ConfigurationElementConfigurationSection,以便在启动时更轻松地设置大量应用程序参数。但是,我真的很想对这个逻辑进行单元测试。

服务连接

public class ServiceConnection : ConfigurationElement
{
    [ConfigurationProperty("locationNumber", IsRequired = true)] 
    public string LocationNumber
    {
        get { return (string) base["locationNumber"]; }
        set { base["locationNumber"] = value; }
    }

    [ConfigurationProperty("hostName", IsRequired = true)]
    public string HostName
    {
        get { return (string) base["hostName"]; }
        set { base["hostName"] = value; }
    }

    [ConfigurationProperty("port", IsRequired = true)]
    public int Port
    {
        get { return (int) base["port"]; }
        set { base["port"] = value; }
    }

    [ConfigurationProperty("environment", IsRequired = true)]
    public string Environment
    {
        get { return (string) base["environment"]; }
        set { base["environment"] = value.ToUpper(); }
    }

    internal string Key
    {
        get { return string.Format("{0}|{1}", LocationNumber, Environment); }
    }
}

ServiceConnection 集合

[ConfigurationCollection(typeof(ServiceConnection), AddItemName = "service", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class ServiceConnectionCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new ServiceConnection();
    }

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

    public ServiceConnection Get(string locationNumber, string environment = "PRODUCTION")
    {
        return (ServiceConnection) BaseGet(string.Format("{0}|{1}", locationNumber, environment));
    }

    public ServiceConnection this[int index]
    {
        get { return (ServiceConnection)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }
}

一些测试 XML

<MyServiceConnections>
    <service locationNumber="0AB0" hostName="DEVSERVER" port="1234" environment="DEVELOPMENT" />
    <service locationNumber="0AB0" hostName="BETASERVER" port="1234" environment="BETA" />
    <service locationNumber="0AB0" hostName="PRODSERVER" port="1234" environment="PRODUCTION" />
</MyServiceConnections>

在我的生产代码中,我使用ConfigurationManager 来检索ServiceConnection,但不知道如何创建一个完全绕过管理器的测试。

我正在寻找一个 ServiceConnection 对象,并确保所有字段都与我在测试 XML 中设置的输入相匹配。当用户未能填充一个或多个字段时,我还想测试功能。

【问题讨论】:

  • 你能解释一下你到底想测试什么吗?我会从实际实现中提取 ConfigurationManager 代码并将其注入。
  • @CodeCaster:已编辑。 “我正在寻找一个 ServiceConnection 对象,并确保所有字段都与我在测试 XML 中设置的输入相匹配。当用户无法填充一个或多个字段时,我还想测试功能。”前段时间我在另一个项目中使用过这段代码,当针对实时 App.config 运行并尝试使用ConfigurationManager 时,在构建服务器(TeamCity)上遇到了问题。如果可能的话,我想完全绕过它。不确定XmlReader 是否正确。
  • 一种在没有 ConfigurationManager.GetSection(和文件配置)的情况下进行测试的方法,使用一个以字符串为参数的改编部分构造函数:stackoverflow.com/a/704817/954701

标签: c# unit-testing nunit configuration-files rhino-mocks


【解决方案1】:

好吧,我最终只是按照@CodeCaster 的建议使用了ConfigurationManager(正如他的链接here 所建议的那样)。

我在下面发布了一个示例测试:

[Test]
public void ShouldProvideFullProductionServiceConnectionRecord()
{
    //NOTE: Open ConfigTests.config in this project to see available ServiceConnection records

    //Arrange
    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "ConfigTests.config" };
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

    ServiceConnectionSection section = config.GetSection("AutomationPressDataCollectionServiceConnections") as ServiceConnectionSection;

    //Act
    var productionSection = section.Connections.Get("0Q8");

    //Assert
    Assert.AreEqual("0AB0", productionSection.LocationNumber);
    Assert.AreEqual("DEVSERVER", productionSection.HostName);
    Assert.AreEqual(1234, productionSection.Port);
    Assert.AreEqual("DEVELOPMENT", productionSection.Environment);
}

它要求您添加一个新的.Config 文件并将其输出设置为Content 并设置为Copy if Newer(它在一个单元测试项目中)。但总比没有报道要好。

【讨论】:

    猜你喜欢
    • 2010-11-01
    • 2011-11-05
    • 2018-02-25
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多