Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单元测试自定义配置元素&;配置元素集合_C#_Unit Testing_Nunit_Configuration Files_Rhino Mocks - Fatal编程技术网

C# 单元测试自定义配置元素&;配置元素集合

C# 单元测试自定义配置元素&;配置元素集合,c#,unit-testing,nunit,configuration-files,rhino-mocks,C#,Unit Testing,Nunit,Configuration Files,Rhino Mocks,我已经创建了一个自定义的ConfigurationElement和ConfigurationSection,以便于在启动时设置大量应用程序参数。然而,我真的想对这个逻辑进行单元测试 服务连接 public class ServiceConnection : ConfigurationElement { [ConfigurationProperty("locationNumber", IsRequired = true)] public string LocationNumber

我已经创建了一个自定义的
ConfigurationElement
ConfigurationSection
,以便于在启动时设置大量应用程序参数。然而,我真的想对这个逻辑进行单元测试

服务连接

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); }
    }
}
[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);
        }
    }
}
服务连接集合

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); }
    }
}
[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中设置的输入匹配。我还想在用户未能填充一个或多个字段时测试功能。

好吧,我最终只是按照@CodeCaster的建议使用了
ConfigurationManager
(根据他的链接建议)

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

[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
(在单元测试项目中)。但总比完全没有覆盖要好。

你能解释一下你到底想测试什么吗?我将从实际实现中提取ConfigurationManager代码并注入该代码。@CodeCaster:Edited。“我希望检索ServiceConnection对象,并确保所有字段与我在测试XML中设置的输入匹配。我还希望在用户未能填充一个或多个字段时测试功能。”不久前,我在另一个项目中使用了此代码,并在生成服务器(TeamCity)上遇到问题在运行live App.config并尝试使用
ConfigurationManager
时。如果可能的话,我想完全绕过它。不确定
XmlReader
是否正确。一种不使用ConfigurationManager.GetSection(和文件配置)的测试方法,使用以字符串为参数的自适应节构造函数: