| | | 1 | | #region Usings |
| | | 2 | | using Microsoft.AspNetCore.Hosting; |
| | | 3 | | #endregion |
| | | 4 | | |
| | | 5 | | namespace ZhEaIsNsAaBn.IntegrationTestingBases; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// <see cref="WebApplicationFactory{TEntryPoint}"/> that runs the app under test in a given environment and overrides i |
| | | 9 | | /// connection string and any other configuration values. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="TProgram">The <c>Program</c> class of the web application under test.</typeparam> |
| | | 12 | | public class CustomWebApplicationFactory< TProgram > : WebApplicationFactory<TProgram> |
| | | 13 | | where TProgram : class |
| | | 14 | | { |
| | | 15 | | /// <summary>The connection-string name used when none is given: <c>DefaultConnection</c>.</summary> |
| | | 16 | | public const string DefaultConnectionStringName = "DefaultConnection"; |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | private readonly Dictionary<string, string> _additionalConfiguration; |
| | | 20 | | private readonly string _connectionString; |
| | | 21 | | private readonly string _connectionStringName; |
| | | 22 | | private readonly string _environment; |
| | | 23 | | |
| | | 24 | | |
| | | 25 | | /// <summary>Creates the factory.</summary> |
| | | 26 | | /// <param name="connectionString">Connection string for the app's database. Not applied when empty.</param> |
| | | 27 | | /// <param name="environment">Hosting environment name. Default: <c>Testing</c>.</param> |
| | | 28 | | /// <param name="additionalConfiguration">Extra configuration values (key = configuration path). Default: none.</par |
| | | 29 | | /// <param name="connectionStringName"> |
| | | 30 | | /// The name the app reads with <c>GetConnectionString( name )</c>; the value is set at <c>ConnectionStrings:{name}< |
| | | 31 | | /// Default: <see cref="DefaultConnectionStringName"/>. |
| | | 32 | | /// </param> |
| | 15 | 33 | | public CustomWebApplicationFactory( |
| | 15 | 34 | | string connectionString, |
| | 15 | 35 | | string environment = "Testing", |
| | 15 | 36 | | Dictionary<string, string>? additionalConfiguration = null, |
| | 15 | 37 | | string connectionStringName = DefaultConnectionStringName |
| | 15 | 38 | | ) |
| | | 39 | | { |
| | 15 | 40 | | _connectionString = connectionString; |
| | 15 | 41 | | _environment = environment; |
| | 15 | 42 | | _additionalConfiguration = additionalConfiguration ?? []; |
| | 15 | 43 | | _connectionStringName = connectionStringName; |
| | 15 | 44 | | } |
| | | 45 | | |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | protected override void ConfigureWebHost( IWebHostBuilder builder ) |
| | | 49 | | { |
| | 15 | 50 | | builder.UseEnvironment( _environment ); |
| | | 51 | | |
| | | 52 | | |
| | 15 | 53 | | builder.ConfigureAppConfiguration( ( _, config ) => |
| | 15 | 54 | | { |
| | 15 | 55 | | var values = new Dictionary<string, string?>( _additionalConfiguration! ); |
| | 15 | 56 | | |
| | 15 | 57 | | |
| | 15 | 58 | | if ( !string.IsNullOrEmpty( _connectionString ) ) { values[ $"ConnectionStrings:{_connectionStringName}" ] = |
| | 15 | 59 | | |
| | 15 | 60 | | |
| | 15 | 61 | | config.AddInMemoryCollection( values ); |
| | 15 | 62 | | } ); |
| | 15 | 63 | | } |
| | | 64 | | } |