SparkyTestHelpers.AppSettings
1.0.0
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
<PackageReference Include="SparkyTestHelpers.AppSettings" Version="1.0.0" />
paket add SparkyTestHelpers.AppSettings --version 1.0.0
#r "nuget: SparkyTestHelpers.AppSettings, 1.0.0"
// 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:
- the rest of the "Sparky suite" of .NET utilities and test helpers
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 | Versions 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. |
-
.NETStandard 2.0
- SparkyTestHelpers (>= 1.2.2)
- SparkyTools.DependencyProvider (>= 1.0.0)
- System.Configuration.ConfigurationManager (>= 4.4.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.