ConfigurationManager.AppSettings Returns Null In Unit Test Project

前端 未结 4 441
故里飘歌
故里飘歌 2021-02-05 05:47

I have a C# unit test project with application settings in the app.config file. I am testing a class that exists in a different project. That class depends on bot

4条回答
  •  灰色年华
    2021-02-05 06:31

    You mentioned settings in the project properties. See if you can access the setting this way:

    string test = Properties.Settings.Default.Bing_Key;
    

    You may need to get the executing assembly of where the project settings file is defined, but try this first.

    EDIT

    When using Visual Studio's project settings file, it adds stuff to your app.config and creates the app.config if it is not present. ConfigurationManager CAN'T touch these settings! You can only get to these specific generated project.settings file from using the above static method. If you want to use ConfigurationManager, you will need to hand write your app.config. Add your settings to it like so:

    
      
    
    

提交回复
热议问题