Last active
June 29, 2020 08:42
-
-
Save martinsmith1968/9567de76d2bbe537af05d76eb39b1162 to your computer and use it in GitHub Desktop.
Convenient IConfiguration for Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DNX.ExtendedMemoryConfiguration | |
{ | |
public class ExtendedMemoryConfigurationSource : IConfigurationSource | |
{ | |
/// <summary> | |
/// The initial key value configuration pairs. | |
/// </summary> | |
public IDictionary<string, string> Data { get; set; } | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ExtendedMemoryConfigurationSource" /> class. | |
/// </summary> | |
/// <param name="data">The data.</param> | |
public ExtendedMemoryConfigurationSource(IDictionary<string, string> data) | |
{ | |
Data = data; | |
} | |
/// <summary> | |
/// Builds the <see cref="MemoryConfigurationProvider"/> for this source. | |
/// </summary> | |
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param> | |
/// <returns>A <see cref="MemoryConfigurationProvider"/></returns> | |
public IConfigurationProvider Build(IConfigurationBuilder builder) | |
{ | |
return new ExtendedMemoryConfigurationProvider(this); | |
} | |
} | |
public class ExtendedMemoryConfigurationProvider : ConfigurationProvider | |
{ | |
private readonly ExtendedMemoryConfigurationSource _source; | |
public ExtendedMemoryConfigurationProvider(ExtendedMemoryConfigurationSource source) | |
{ | |
if (source == null) | |
{ | |
throw new ArgumentNullException(nameof(source)); | |
} | |
_source = source; | |
if (_source.Data != null) | |
{ | |
LoadData(); | |
} | |
} | |
public override void Load() | |
{ | |
base.Load(); | |
LoadData(); | |
} | |
private void LoadData() | |
{ | |
if (_source.Data != null) | |
{ | |
foreach (var pair in _source.Data) | |
{ | |
Data[pair.Key] = pair.Value; | |
} | |
} | |
} | |
/// <summary> | |
/// Returns an enumerator that iterates through the collection. | |
/// </summary> | |
/// <returns>An enumerator that can be used to iterate through the collection.</returns> | |
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() | |
{ | |
return Data.GetEnumerator(); | |
} | |
} | |
public static class ExtendedMemoryConfigurationExtensions | |
{ | |
public static IConfigurationBuilder AddExtendedInMemoryCollection( | |
this IConfigurationBuilder configurationBuilder, | |
IDictionary<string, string> dictionary) | |
{ | |
if (configurationBuilder == null) | |
{ | |
throw new ArgumentNullException(nameof(configurationBuilder)); | |
} | |
configurationBuilder.Add(new ExtendedMemoryConfigurationSource(dictionary)); | |
return configurationBuilder; | |
} | |
} | |
public class Tests | |
{ | |
[Fact] | |
public void I_can_fake_configuration_and_amend_inflight() | |
{ | |
// Arrange: Setup default values | |
var values = new Dictionary<string, string>() | |
{ | |
{ "Bob", "Legend" } | |
}; | |
// Act: Build configuration | |
var configuration = new ConfigurationBuilder() | |
.AddExtendedInMemoryCollection(values) | |
.Build(); | |
// Ensure added | |
configuration.Providers.Count().ShouldBe(1); | |
// Assert: Check we can see the initial values | |
var debugView = configuration.GetDebugView(); | |
debugView.Contains("Bob=Legend").ShouldBeTrue(); | |
debugView.Contains("Dave=Hero").ShouldBeFalse(); | |
// Add another value | |
values.Add("Dave", "Hero"); | |
configuration.Reload(); | |
// Assert: Weve added a value after building | |
debugView = configuration.GetDebugView(); | |
debugView.Contains("Dave=Hero").ShouldBeTrue(); | |
// Add values for a class | |
values.Add("Section:Id", "1"); | |
values.Add("Section:Name", "Harry"); | |
configuration.Reload(); | |
// Assert: That we can bind values to classes successfully | |
var myInfo = configuration.GetSection("Section") | |
.Get<MyInfo>(); | |
myInfo.Id.ShouldBe(1); | |
myInfo.Name.ShouldBe("Harry"); | |
} | |
} | |
internal class MyInfo | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment