Joe B
Joe B

Reputation: 788

.net standard ConfigurationManager opens wrong app.config

I'm trying to run a test method from a .net core unit test project over a .net standard project and when loading config file which is in the test project (as this is the current executing assembly). I'm getting a wrong config file with the current file path "C:\Users\xxx\.nuget\packages\microsoft.testplatform.testhost\15.3.0-preview-20170628-02\lib\netstandard1.5\testhost.dll.config"

 var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Upvotes: 1

Views: 846

Answers (2)

fradsham
fradsham

Reputation: 141

You have the option to point to another config file. The credit for the source goes to the Ron Hagerman for his answer with only slight modifications. The following xunit test may help with understanding on how to set the location. It is frustrating that .net core unit tests looks for the connection string in the testhost.dll.config file in the following directory

C:\Users\[UserName]\.nuget\packages\microsoft.testplatform.testhost\[version]\lib\netstandard1.5\testhost.dll.config

[Fact]
public void AccessAppSettings_ConnectionString()
{
     //obtain the current directory for the executable
     Uri UriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly()
                                           .GetName().CodeBase));
     string appPath = UriAssemblyFolder.LocalPath;
     //Change the "DataProvider.Tests.dll" to whatever your 
     //library or executable name. 
     //Note: Configuration manager will add the .config extension
     Configuration config = ConfigurationManager
            .OpenExeConfiguration(appPath + @"\" + "DataProvider.Tests.dll");

     ConnectionStringsSection section = 
                  config.GetSection("connectionStrings") as ConnectionStringsSection;

     string expectedString = $"Data Source=mysqliteDBName.sqlite3";
     //Change "mySqliteConnectionString" to your connection string name   
     var sut_connectionString = 
                 section.ConnectionStrings ["mySqliteConnectionString"].ConnectionString;

      Assert.NotNull(sut_connectionString);
      Assert.Contains(expectedString, sut_connectionString);

Sample Config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <add name="mySqliteConnectionString" connectionString="Data Source=mysqliteDBName.sqlite3" />
        <add name="otherConnectionString" connectionString="Data Source=othersqliteDBName.sqlite3" />
    </connectionStrings>
</configuration>

Upvotes: 1

CubanTurin
CubanTurin

Reputation: 177

I don't think you are getting the wrong one. You are getting the Test project's config file which is the correct one since you are running the Test project.

Every project has it's own config file or none. It cannot just go and load another project's config file.

Bottom line is: just copy whatever specific config you want from your .net standard project's config file to your unit test's config file.

Upvotes: 0

Related Questions