单元测试自定义的ConfigurationElement

单元测试自定义的ConfigurationElement

本文介绍了单元测试自定义的ConfigurationElement&安培; ConfigurationElementCollection中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义 ConfigurationElement ConfigurationSection ,以便更容易设置主机的应用程序参数启动。但是,我真的想单元测试这个逻辑。

I have created a custom ConfigurationElement and ConfigurationSection to make it easier to set up a host of application parameters at startup. However, I'd really like to unit test this logic.

ServiceConnection

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 Collection / p>

ServiceConnection Collection

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

<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 ,但不知道如何创建绕过管理器的测试。

In my production code, I'm using ConfigurationManager to retrieve the ServiceConnection but am not sure how to create a test that bypasses the manager altogether.

I想要检索一个ServiceConnection对象,并确保所有字段都匹配我在测试XML中设置的输入。我也想在用户未能填充一个或多个字段时测试功能。

I'm looking to retrieve a ServiceConnection object and make sure all the fields match the input that I set up in the test XML. I'd also like to test functionality when a user failed to populate one or more of the fields.

推荐答案

(建议使用@CodeCaster建议,并使用 ConfigurationManager (如他的链接

Well, I ended up simply going with what @CodeCaster suggested and using ConfigurationManager anyway (as suggested by his link here).

我已经发布了一个示例测试如下:

I've posted a sample test below:

[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 文件并将其设置为内容并设置为复制如果更新项目)。但它比没有覆盖。

It requires you add a new .Config file and set it's output as Content and set to Copy if Newer (it's in a unit test project). But it's better than having no coverage at all.

这篇关于单元测试自定义的ConfigurationElement&安培; ConfigurationElementCollection中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!