Skip to content

Commit

Permalink
add tests for reading value in OnChange callback
Browse files Browse the repository at this point in the history
  • Loading branch information
dombrovsky committed Oct 23, 2022
1 parent 800c6fd commit 026ad02
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
35 changes: 35 additions & 0 deletions MutableOptions.Tests/MutableOptionsMonitorTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace MutableOptions.Tests
{
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options.Mutable;
using MutableOptions.Tests.TestOptions;
using NUnit.Framework;

[TestFixture]
Expand All @@ -17,5 +20,37 @@ protected override IOptionsMonitor<T> CreateOptionsMonitor<T>(IServiceProvider s
{
return serviceProvider.GetRequiredService<IMutableOptionsMonitor<T>>();
}

[Test]
public void NotifyPropertyChangedWrapper_ReadingCurrentValueFromChangedCallback_ShouldReturnNewValue()
{
var configData = new[]
{
new KeyValuePair<string, string>("StringValue", "foo"),
};

using var host = Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureAppConfiguration(builder => builder.AddInMemoryCollection(configData))
.ConfigureServices((context, services) =>
{
services.ConfigureMutable<SimpleOptions>(context.Configuration);
services.AddSingleton<SimpleSettingsService>();
})
.Build();

var simpleSettingsService = host.Services.GetRequiredService<SimpleSettingsService>();
Assert.That(simpleSettingsService.StringValue, Is.Not.EqualTo("bar"));

var onChangeCalledTimes = 0;
simpleSettingsService.PropertyChanged += (_, _) =>
{
Assert.That(simpleSettingsService.StringValue, Is.EqualTo("bar"));
onChangeCalledTimes++;
};

simpleSettingsService.StringValue = "bar";

Assert.That(onChangeCalledTimes, Is.EqualTo(1));
}
}
}
29 changes: 29 additions & 0 deletions MutableOptions.Tests/OptionsMutatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,35 @@ public void Setup()
.Returns(info => EqualityComparer<SimpleOptions>.Default.Equals(info.ArgAt<SimpleOptions?>(0), info.ArgAt<SimpleOptions?>(1)));
}

[Test]
public void OptionsMonitor_ReadingCurrentValueFromChangedCallback_ShouldReturnNewValue()
{
var configData = new[]
{
new KeyValuePair<string, string>("StringValue", "foo"),
};

using var host = Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureAppConfiguration(builder => builder.AddInMemoryCollection(configData))
.ConfigureServices((context, services) => services.ConfigureMutable<SimpleOptions>(context.Configuration))
.Build();

var optionsMonitor = CreateOptionsMonitor<SimpleOptions>(host.Services);
Assert.That(optionsMonitor.CurrentValue.StringValue, Is.Not.EqualTo("bar"));

var onChangeCalledTimes = 0;
optionsMonitor.OnChange(options =>
{
Assert.That(optionsMonitor.CurrentValue.StringValue, Is.EqualTo("bar").And.EqualTo(options.StringValue));
onChangeCalledTimes++;
});

var optionsMutator = CreateSut<SimpleOptions>(host.Services);
optionsMutator.Mutate(options => options with { StringValue = "bar" });

Assert.That(onChangeCalledTimes, Is.EqualTo(1));
}

[Test]
public void ConfigurationEmpty_WhenMutate_ShouldWriteToConfiguration()
{
Expand Down
35 changes: 35 additions & 0 deletions MutableOptions.Tests/TestOptions/SimpleSettingsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace MutableOptions.Tests.TestOptions
{
using Microsoft.Extensions.Options.Mutable;
using System.ComponentModel;

internal sealed class SimpleSettingsService : INotifyPropertyChanged, IDisposable
{
private readonly IMutableOptionsMonitor<SimpleOptions> _mutableOptionsMonitor;
private readonly IDisposable _changeSubscription;

public SimpleSettingsService(IMutableOptionsMonitor<SimpleOptions> mutableOptionsMonitor)
{
_mutableOptionsMonitor = mutableOptionsMonitor;
_changeSubscription = _mutableOptionsMonitor.OnChange(HandleSimpleOptionsChanged);
}

public event PropertyChangedEventHandler? PropertyChanged;

public string StringValue
{
get => _mutableOptionsMonitor.CurrentValue.StringValue;
set => _mutableOptionsMonitor.Mutate(options => options with { StringValue = value });
}

public void Dispose()
{
_changeSubscription.Dispose();
}

private void HandleSimpleOptionsChanged(SimpleOptions simpleOptions, string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
}
}
}
2 changes: 1 addition & 1 deletion MutableOptions/MutableOptions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>10</LangVersion>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<Deterministic>true</Deterministic>
Expand Down

0 comments on commit 026ad02

Please sign in to comment.