Unit Testing Custom ConfigurationElement & ConfigurationElementCollection

I created custom ConfigurationElement and ConfigurationSection to make it easy to configure many application settings at startup. However, I would 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

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

Some test 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> 

In my production code, I use the ConfigurationManager to retrieve the ServiceConnection , but am not sure how to create a test that generally bypasses the manager.

I am looking to get a ServiceConnection object and make sure all the fields match the input that I set in the test XML. I would also like to test the functionality when the user was unable to fill out one or more fields.

+5
source share
1 answer

Well, I ended up just going with what @CodeCaster suggested and using the ConfigurationManager (as suggested by its link here ).

I posted a test sample 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); } 

To do this, add a new .Config file and install it as Content and install Copy if Newer (it is in the unit test project). But this is better than no coverage at all.

+6
source

Source: https://habr.com/ru/post/1200321/


All Articles