Skip to content

Commit

Permalink
Intercept Reflection exceptions from AppSettingsConfigurationStore
Browse files Browse the repository at this point in the history
Some platforms throws reflection exceptions when trying to use `ConfigurationManager`.
This does not fix the underlying problem of not being unable to use `ConfigurationManager`, but returns `null` instead of an exception.

Relates to #1207 and #1151
  • Loading branch information
jnyrup committed Dec 29, 2019
1 parent f60927f commit 8c97ed4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
@@ -0,0 +1,36 @@
#if !NETSTANDARD1_3 && !NETSTANDARD1_6

namespace FluentAssertions.Common
{
internal class ConfigurationStoreExceptionInterceptor : IConfigurationStore
{
private bool throwsException;

private readonly IConfigurationStore configurationStore;

public ConfigurationStoreExceptionInterceptor(IConfigurationStore configurationStore)
{
this.configurationStore = configurationStore;
}

public string GetSetting(string name)
{
if (throwsException)
{
return null;
}

try
{
return configurationStore.GetSetting(name);
}
catch
{
throwsException = true;
return null;
}
}
}
}

#endif
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Common/Services.cs
Expand Up @@ -48,7 +48,7 @@ public static void ResetToDefaults()
ConfigurationStore = new NullConfigurationStore();
#else
Reflector = new FullFrameworkReflector();
ConfigurationStore = new AppSettingsConfigurationStore();
ConfigurationStore = new ConfigurationStoreExceptionInterceptor(new AppSettingsConfigurationStore());
#endif

ThrowException = TestFrameworkProvider.Throw;
Expand Down

0 comments on commit 8c97ed4

Please sign in to comment.