0

I have been reading as much as I can about how to test a method that itself reads from app.config.

I have read through Moq, but this seems to require everything to be an interface, and then I have read that it is possible to read a different app.config, ie the one that contains test data.

My code simplified to the relevant bits is something along the lines of.

public class myclass
{
    public void doStuff
    {
        ConfigurationManager.RefreshSection("appSettings");
        outputFolder = ConfigurationManager.AppSettings["OutputFolder"];
        
        FileSystemConfiguration fsc = (FileSystemConfiguration) ConfigurationManager.GetSection("Configurables/Folders");
        
        // ......
    }
}

public class FileSystemConfiguration : ConfigurationSection
{
    [ConfigurationProperty("Folder")]
    public FolderCollection FolderItems) { get { return ((FolderCollection)(base["Folder"])); } } 
    
    [ConfigurationCollection(typeof(FolderElement))]
    public class FolderCollection :ConfigurationElementCollection 
    {
        public FolderElement this[int idx] { get {return (FolderElement)BaseGet(idx); } }
       
        // ......
    }
}

What I cannot work out is how to set up the unittest code

[DeploymentItem(@"Config\Testing.config")]
public void TestMethod()
{
    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()
                                                { 
                                                     ExeConfigFileName = path.combine(TestContext.DeploymentDirectory, "Testing.config")
                                                };

    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap,ConfigurationUserLevel.None

At this point I am lost as to how to get the config that I have just read into the myclass such that the code treats it as the app.config.

I have tried googling and found some articles, but those that I have found lack the full code sample so I've not, so far, been able to join everything together.

Links to existing example appreciated, or any guidance as how how I get the config that I have just read fed into my class such the ConfigurationManager.AppSettings is read from the test config.

If it is actually possible to Moq the above then that would be good to understand, again happy with links, but they need to be full examples so that they make some sense.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Duplicates: [1](https://stackoverflow.com/questions/344069/can-a-unit-test-project-load-the-target-applications-app-config-file), [2](https://stackoverflow.com/questions/65332017/how-do-i-get-a-c-sharp-unit-test-to-use-app-config), [3](https://stackoverflow.com/questions/48666780/cant-read-app-config-in-c-sharp-net-core-unit-test-project-with-configurationm), [4](https://stackoverflow.com/questions/5674619/app-config-in-test-projects), etc. – Hans Passant Jul 10 '23 at 15:05
  • Many thanks for these links 1) These seems to copy a link to the app config, so not an option 2) Lacks the necessary level of information. I've got that far, but cannot work out the next set of steps 3) Seems to work, which is great, so many thanks. 4) Seems to be the same as 1 – tractor boy Jul 11 '23 at 09:28

0 Answers0