SparkyTestHelpers.AppSettings 1.0.5

Install-Package SparkyTestHelpers.AppSettings -Version 1.0.5
dotnet add package SparkyTestHelpers.AppSettings --version 1.0.5
<PackageReference Include="SparkyTestHelpers.AppSettings" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.AppSettings --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SparkyTestHelpers.AppSettings, 1.0.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.5

// Install SparkyTestHelpers.AppSettings as a Cake Tool
#tool nuget:?package=SparkyTestHelpers.AppSettings&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

see also:


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
        .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...


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

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 156 4/4/2021
1.0.4 311 7/27/2019
1.0.3 256 7/27/2019
1.0.1 460 12/2/2018
1.0.0 591 2/22/2018