SparkyTestHelpers.AppSettings 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SparkyTestHelpers.AppSettings --version 1.0.0                
NuGet\Install-Package SparkyTestHelpers.AppSettings -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SparkyTestHelpers.AppSettings" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.AppSettings --version 1.0.0                
#r "nuget: SparkyTestHelpers.AppSettings, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SparkyTestHelpers.AppSettings as a Cake Addin
#addin nuget:?package=SparkyTestHelpers.AppSettings&version=1.0.0

// Install SparkyTestHelpers.AppSettings as a Cake Tool
#tool nuget:?package=SparkyTestHelpers.AppSettings&version=1.0.0                

see also:

AppSettingsHelper

If you need to test code that directly uses ConfigurationManager.AppSettings to retrieve .config file values, your test code can say:

    ConfigurationManager.AppSettings["key"] = "test value";

...but that leaves your test value in the static ConfigurationManager.AppSettings collection, which can cause problems with tests of other code using the same app setting.

This class lets you test with one or more AppSettings overrides (WithAppSetting is required. You can use zero or as many AndAppSetting clauses as you need), and automatically undo the overrides when the test is complete:

using SparkyTestHelpers.AppSettings
...
    AppSettingsHelper
        .WithAppSetting("key1", "test value 1")
        .AndAppSetting("key2", "test value 2")
        .Test(() =>
        {
            // Code to test method that uses "key1"/"key2" 
            // ConfigurationManager.AppSettings values
        });

But there's a better way...

AppSettings.DependencyProvider

Code with static dependencies can be made more testable by using a dependency-injectable wrapper around the static code. SparkyTools.DependencyProvider provides one way to do this.

SparkyTools.XmlConfig has an AppSettings.DependencyProvider() method that creates a DependencyProvider wrapping ConfigurationManager.AppSettings, and this package has an AppSettings.TestDependencyProvider() method that creates a provider that serves from a key/value dictionary:

Class using DependencyProvider:

using SparkyTools.DependencyProvider;

public class Foo
{
    private readonly Func<string, string> _getAppSetting;

    public Foo(DependencyProvider<Func<string, string> appSettingsProvider)
    {
        _getAppSetting = appSettingsProvider.GetValue();
    }

    public void MethodUsingAppSettings()
    {
        string valueFromAppSettings = _getAppSetting("bar");
    }
}

Production code using ConfigurationManager.AppSettings DependencyProvider:

using SparkyTools.XmlConfig;
...
    var foo = new Foo(AppSettings.DependencyProvider());

Unit test code using dictionary DependencyProvider:

using SparkyTestHelpers.AppSettings;
...
    var foo = new Foo(AppSettings.DependencyProvider(
        new Dictionary<string, string>{ { "bar", "test bar value" } });
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 547 4/4/2021
1.0.4 587 7/27/2019
1.0.3 557 7/27/2019
1.0.1 735 12/2/2018
1.0.0 1,016 2/22/2018