Unit Testing custom ConfigurationElement & ConfigurationElementCollection

前端 未结 1 1813
醉梦人生
醉梦人生 2021-02-09 12:49

I have created a custom ConfigurationElement and ConfigurationSection to make it easier to set up a host of application parameters at startup. However,

1条回答
  •  情书的邮戳
    2021-02-09 13:42

    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.

    0 讨论(0)
提交回复
热议问题