如何从WebApplicationFactory获取配置?

9

我正在编写一个集成测试,使用WebApplicationFactory 来测试我的APIs。

我已经创建了一个CustomWebApplicationFactory。我想要读取位于主web项目中的默认appsettings.json。我知道可以通过以下方式实现:

private static readonly IConfigurationRoot _configuration; // Global

// Inside Constructor
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)

 _configuration = builder.Build();

// To get connectionstring

var connStr = _configuration.GetConnectionString("DefaultConnection");

在我的情况下,我想默认使用 appsettings.json 。有没有一种方法可以从主 Web 项目中读取 appsettings.json 获取连接字符串,而不需要在构造函数中定义 AddJsonFile 代码?

3个回答

5
你可以按照以下方式实现(请参考下面的代码)。即拦截构建器并以与服务相同的方式构建配置。
public class MyApiServiceWrapper : WebApplicationFactory<Api.Startup>
{
    private IConfiguration _configuration;

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureAppConfiguration((context, conf) =>
        {
            // expand default config with settings designed for Integration Tests
            conf.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "appsettings.Integration.json"));
            conf.AddEnvironmentVariables();

            // here we can "compile" the settings. Api.Setup will do the same, it doesn't matter.
            _configuration = conf.Build();
        });

        builder.ConfigureTestServices(srv =>
        {
            // here you can change some internal services of the Api.Startup
            // and change them to mocks
        });

        base.ConfigureWebHost(builder);
    }

    public TConfig GetConfiguration<TConfig>()
    {
        // start my service to get the config built...
        this.CreateClient(); 
        // ... and then cast it to my type
        return _configuratio.Get<TConfig>();
    }
}

1
自从我将WebApplicationFactory<Program>注入到我的集成测试类中,我使用以下内容:
public class MyIntegrationTests : IClassFixture<WebApplicationFactory<Program>>
{
  private readonly WebApplicationFactory<Program> _app;
  private readonly IConfiguration? _config;
  
  public MyIntegrationTests(WebApplicationFactory<Program> app) 
  {
    _app = app;
    _config = app.Services.GetService<IConfiguration>();
  }

}

这对我有效,应该是答案。 - undefined

0

对于以下设置:

public class Fixtures
{
    public readonly WebApplicationFactory<WebApplication1.Program> Factory { get; }
    
    public Fixtures()
    {
        Factory = new WebApplicationFactory<Startup>().WithWebHostBuilder(builder =>
        {
            builder.ConfigureServices(services =>
            {
                services.Configure<YourSettings>(settings =>
                {
                    settings.Setting1 = "OverrideSetting1"
                });
            });
        });
    }
}

如果您使用选项模式,您可以尝试使用GetService<T>方法从IServiceProvider请求您的设置:
public class Test
{
    private readonly YourSettings _settings;
    
    public Test(Fixtures fixtures)
    {
        var settingsOptions = fixtures.Factory.Services.GetService<IOptions<YourSettings>>();
        _settings = settingsOptions?.Value;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接