Skip to content

Commit

Permalink
Remove Moq from ConfigBinder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericstj authored and github-actions committed Sep 22, 2023
1 parent 2a8cdd4 commit fdc4a25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#if BUILDING_SOURCE_GENERATOR_TESTS
using Microsoft.Extensions.Configuration;
#endif
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.Configuration.Test;
using Moq;
using Xunit;

namespace Microsoft.Extensions
Expand Down Expand Up @@ -2406,28 +2406,37 @@ public void SharedChildInstance()
Assert.Equal("localhost", instance.ConnectionString);
}

// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
public void CanBindToMockConfiugrationSection()
[Fact]
public void CanBindToMockConfigurationSection()
{
const string expectedA = "hello";

var mockConfSection = new Mock<IConfigurationSection>();
var aSection = new Mock<IConfigurationSection>();
aSection.Setup(m => m.Value).Returns(expectedA);

// only mock one of the two properties, the other will return a null section.
// runtime binder uses GetSection
mockConfSection.Setup(config => config.GetSection(nameof(SimplePoco.A))).Returns(aSection.Object);
// source gen uses indexer
mockConfSection.Setup(config => config[nameof(SimplePoco.A)]).Returns(aSection.Object?.Value);
mockConfSection.Setup(config => config.GetChildren()).Returns(new[] { aSection.Object });
var configSource = new MemoryConfigurationSource()
{
InitialData = new Dictionary<string, string?>()
{
[$":{nameof(SimplePoco.A)}"] = expectedA,
}
};
var configRoot = new MockConfigurationRoot(new[] { configSource.Build(null) });
var configSection = new ConfigurationSection(configRoot, string.Empty);

SimplePoco result = new();
mockConfSection.Object.Bind(result);
configSection.Bind(result);

Assert.Equal(expectedA, result.A);
Assert.Equal(default(string), result.B);
}

// a mock configuration root that will return null for undefined Sections,
// as is common when Configuration interfaces are mocked
class MockConfigurationRoot : ConfigurationRoot, IConfigurationRoot
{
public MockConfigurationRoot(IList<IConfigurationProvider> providers) : base(providers)
{ }

IConfigurationSection IConfiguration.GetSection(string key) =>
this[key] is null ? null : new ConfigurationSection(this, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options.ConfigurationExtensions\src\Microsoft.Extensions.Options.ConfigurationExtensions.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.Binder.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Json\src\Microsoft.Extensions.Configuration.Json.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.FileProviders.Abstractions\src\Microsoft.Extensions.FileProviders.Abstractions.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.Binder.csproj" SkipUseReferenceAssembly="true" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

</Project>

0 comments on commit fdc4a25

Please sign in to comment.