SparkyTestHelpers.AppSettings 1.0.4

Helpers for testing code that uses ConfigurationManager.AppSettings

Install-Package SparkyTestHelpers.AppSettings -Version 1.0.4
dotnet add package SparkyTestHelpers.AppSettings --version 1.0.4
<PackageReference Include="SparkyTestHelpers.AppSettings" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.AppSettings --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

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

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.4 67 7/27/2019
1.0.3 58 7/27/2019
1.0.1 160 12/2/2018
1.0.0 273 2/22/2018