1

I have created a class library MultipleConfigFiles, which has a method getConfigData. This method reads data from config files.

 public class Class1
    {
      public string getConfigData()
      {
        string configData = ConfigurationManager.AppSettings["key"].ToString();
        return configData;
      }
    }

Now I have a console application ConsoleAppConfig, in which I have given the reference of the class library.In the main method, I have created a object for the library class and called the method.

static void Main(string[] args)
        {
            Class1 c = new Class1();
            string data = c.getConfigData();
            Console.Write(data);    
        }

This console application has 2 different config files dev.config and prod.config. In the main App.config file , I have used configSource to map the appropriate config file.

<appSettings configSource="dev.config" />

Dev.config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="key" value="dev" />
</appSettings>

Till here it is working fine.That is, when I run the console app, the method returns dev.

Now,I have created a MSTest project for this class library.

public class Class1Tests
    {
        [TestMethod()]
        public void getConfigDataTest()
        {
            Class1 obj = new Class1();
            var result = obj.getConfigData();                
        }
    }

Now the problem is, when I run this test, it throws an error saying
"An exception of type 'System.NullReferenceException' occurred in MultipleConfigFiles.dll but was not handled in user code. Additional information: Object reference not set to an instance of an object."

Since the config files are in ConsoleApplication , the method is not able to read the appsetting variables from there. Is there any way I can maintain the configuration files globally so that both of them use individually , or is there any better way to handle this.

Any inputs are appreciated.Thanks.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • You need to have app.config file in the UnitTest Project. When you run UnitTests it will not use the app.config of Console Application project. – Chetan Jun 19 '18 at 06:02
  • Should I add just the app.config file or even dev.config and prod.config. In anycase, those files will be duplicated.If somebody changes in a config file in consoleapplication and forgot to do that in TestProject, there will be a ambiguity, which I want to avoid. So is there any other way where these files are maintained globally? – CrazyCoder Jun 19 '18 at 06:07
  • If you want to run unit tests with some specific values of `AppSettgings` then you just need app.config and have values put in `` of app.config itself. If you want to run unit tests with data of dev and prod config files then you need to add all three files in the test project. – Chetan Jun 19 '18 at 06:09
  • @CrazyCoder as an addition: you do not need to duplicate the files. Have a look at using ['Add as link' in Visual Studio](https://blogs.msdn.microsoft.com/zainnab/2010/11/12/linked-items-in-projects/). – rickvdbosch Jun 19 '18 at 06:20
  • If you make a shortcut file it won't work, most people make copies (https://stackoverflow.com/a/11535980/495455), the better more time consuming way is to mock the ConfigurationManager. – Jeremy Thompson Jun 19 '18 at 06:46
  • @JeremyThompson Can you show me how to mock the configurationmanager. Any example or sample code would be helpful – CrazyCoder Jun 19 '18 at 06:59
  • @rickvdbosch I tried adding those files as links. It was giving an error "Unable to open configSource file 'dev.config'." – CrazyCoder Jun 19 '18 at 07:00
  • 1
    You're going to have to add all of the files, and set them to be copied on build under Properties -> Copy to Output Directory. – rickvdbosch Jun 19 '18 at 07:03
  • @rickvdbosch . In properties , there is no Copied on build. So I set them to Copy always. It worked. Thanks a lot. – CrazyCoder Jun 19 '18 at 07:08
  • Hey, I will reopen if you need,, I didn't know YOU CAN ADD CONFIG TO OTHER PROJECTS AS LINKS! – Jeremy Thompson Jun 19 '18 at 08:23
  • Weird even a trusted c# user I cannot close this as a duplicate: https://stackoverflow.com/a/1528427/495455 – Jeremy Thompson Jun 19 '18 at 08:24
  • Closed in web browser as a duplicate, didn't work in SO app. Tell me if you want me to reopen. I don't want to hammer close this :) – Jeremy Thompson Jun 19 '18 at 08:26

1 Answers1

0

Without duplicating the files you can the Link the files in existing Unit Test project. You can try the same as mentioned in the image Link (https://grantwinney.com/content/images/2014/04/AddExistingFileAsLink-002.png)

Google this term "Visual Studio - Add File As Link" for more Info

Image Credits : https://grantwinney.com

Bhuvan Ram
  • 20
  • 5