gpt4 book ai didi

c# - 单元测试自定义 ConfigurationElement 和 ConfigurationElementCollection

转载 作者:太空狗 更新时间:2023-10-29 20:13:27 25 4
gpt4 key购买 nike

我创建了自定义 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 中设置的输入相匹配。我还想在用户未能填写一个或多个字段时测试功能。

最佳答案

好吧,我最终只是简单地按照@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 (它在一个单元中测试项目)。但这总比完全没有覆盖要好。

关于c# - 单元测试自定义 ConfigurationElement 和 ConfigurationElementCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25308733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com