无法让ConfigurationBuilder从ASP.NET Core 1.0 RC2中的xunit类库中的appsettings.json读取

时间:2020-11-29 20:23:56

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

我试图在XUnit项目中执行以下操作,以获取我的测试应该使用的数据库的连接字符串:

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.

这很奇怪,因为它在Startup.cs中使用时在WebAPI和MVC模板中完美运行。此外,此代码以前在RC1中使用dnx,但现在我将所有内容更新为RC2和Core CLI,它不再能够找到位于xunit类库根目录中的appsettings.json文件。

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);
    }
}

2 个解决方案

#1


17  

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.

这是一个简单的问题,您需要在xunit项目下存在appsetting.json。或者您需要使用指向appsettings.json在另一个项目中的位置的相对路径。

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:

理想情况下,您只需拥有自己的测试项目本地配置文件。在我的ASP.NET Core RC2数据库测试项目中,我的fixture看起来像这样:

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

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

testsettings.json是测试特定的配置,在项目的本地。

Update

更新

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

在project.json中,确保将appsettings.json标记为copyToOutput。查看架构存储以获取详细信息。

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

#2


3  

To add more information for @DavidPine 's answer

为@DavidPine的答案添加更多信息

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

我的是.net core 1.0.0-preview1-002702

#1


17  

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.

这是一个简单的问题,您需要在xunit项目下存在appsetting.json。或者您需要使用指向appsettings.json在另一个项目中的位置的相对路径。

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:

理想情况下,您只需拥有自己的测试项目本地配置文件。在我的ASP.NET Core RC2数据库测试项目中,我的fixture看起来像这样:

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

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

testsettings.json是测试特定的配置,在项目的本地。

Update

更新

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

在project.json中,确保将appsettings.json标记为copyToOutput。查看架构存储以获取详细信息。

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

#2


3  

To add more information for @DavidPine 's answer

为@DavidPine的答案添加更多信息

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

我的是.net core 1.0.0-preview1-002702