I have created a custom ConfigurationElement
and ConfigurationSection
to make it easier to set up a host of application parameters at startup. However,
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);
}
It requires you add a new .Config
file and set its 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.