Unable to get ConfigurationBuilder to read from appsettings.json in xunit class library in ASP.NET Core 1.0 RC2

11,918

Solution 1

This is a simple issue, you need to have the appsetting.json also exist under the xunit project. Or you need to use a relative path pointing to where the appsettings.json exists in the other project.

public class TestFixture : IDisposable
{
    public IConfigurationRoot Configuration { get; set; }
    public MyFixture()
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("../../OtherProj/src/OtherProj/appsettings.json", 
                         optional: true, reloadOnChange: true);
        Configuration = builder.Build();
    }

    public void Dispose() {  }
}

Ideally, you'd simply have your own config file local to the test project. In my ASP.NET Core RC2 database test project, my fixture looks like this:

public DatabaseFixture()
{
    var builder =
        new ConfigurationBuilder()
            .AddJsonFile("testsettings.json")
            .AddEnvironmentVariables();
   // Omitted...
}

Where the testsettings.json is the test specific config, local to the project.

Update

In your project.json ensure that you're marking the appsettings.json as copyToOutput. Look at the schema store for the details.

"buildOptions": {
  "copyToOutput": {
    "include": [ "appsettings.json" ]
  }
},

Solution 2

To add more information for @DavidPine 's answer

Somehow relative path doesn't work for me so here is how I do it.

var builder = new ConfigurationBuilder()
.SetBasePath(Path.GetFullPath(@"../XXXX")).AddJsonFile("appsettings.json");

Mine is .net core 1.0.0-preview1-002702

Share:
11,918

Related videos on Youtube

Blake Rivell
Author by

Blake Rivell

I am a .NET developer who builds custom web applications for businesses. I have a very strong passion for what I do. My hobbies are video games and fitness.

Updated on June 19, 2022

Comments

  • Blake Rivell
    Blake Rivell almost 2 years

    I am trying to do the following in an XUnit project to get the connectionstring to the database my tests should be using:

    public class TestFixture : IDisposable
    {
        public IConfigurationRoot Configuration { get; set; }
        public MyFixture()
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            Configuration = builder.Build();
        }
    
        public void Dispose()
        {    
        }
    }
    

    It is very strange because it works perfectly in the WebAPI and MVC templates when used in Startup.cs. Additionally, this code was previously working in RC1 with dnx, but now that I updated everything to RC2 and Core CLI it no longer is able to find the appsettings.json file which is in the root of the xunit class library.

    Here is what my test class looks like so you can see how I am calling for the configuration:

    public class MyTests : IClassFixture<MyFixture>
    {
        private readonly MyFixture _fixture;
        public MyTests(MyFixture fixture)
        {
            this._fixture = fixture;
        }
    
        [Fact]
        public void TestCase1()
        {
            ICarRepository carRepository = new CarRepository(_fixture.Configuration);
        }
    }
    
  • Blake Rivell
    Blake Rivell almost 8 years
    Maybe my issue is something else, because my appsettings.json is inside the root of my xunit project. I said that in my post: 'it no longer is able to find the appsettings.json file which is in the root of the xunit class library.' Ever since I switched to the new RC2 template it started failing. What packages do you have installed in your project.json file if you don't mind me asking, in order to have the line of code you pasted. I want to make sure I am using the same ones.
  • Blake Rivell
    Blake Rivell almost 8 years
    I read about the build options setting in the new xunit docs but didn't realize it had to be done for appsettings.json too. Yes, that would be excellent if you could show me how to do it.
  • Blake Rivell
    Blake Rivell almost 8 years
    Thanks! One last question: Do you use Microsoft.Extensions.Configuration or Microsoft.Extensions.Configuration.Abstractions when referencing IConfigurationRoot from a class library. Both seem to work.
  • David Pine
    David Pine almost 8 years
    Yes, you should use both. One is the extension methods which are useful, and the other is the abstraction of the types.
  • leon
    leon almost 8 years
    The update (copyToOutput) allowed me to finally read the *setings.json files I have into my strongly typed options. This was the only place that this step is mentioned as far as I can see.

Related