Skip to content

Commit

Permalink
Add unit tests for TestFrameworkProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
lg2de committed Jan 14, 2023
1 parent 14891cc commit f9a958a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
7 changes: 6 additions & 1 deletion Src/FluentAssertions/Common/Configuration.cs
Expand Up @@ -5,6 +5,11 @@ namespace FluentAssertions.Common;

public class Configuration
{
/// <summary>
/// Defines the key for the configuration of the test framework to be assumed in FluentAssertions.
/// </summary>
private const string TestFrameworkConfigurationKey = "FluentAssertions.TestFramework";

#region Private Definitions

private readonly object propertiesAccessLock = new();
Expand Down Expand Up @@ -118,7 +123,7 @@ public string TestFrameworkName
{
if (string.IsNullOrEmpty(testFrameworkName))
{
testFrameworkName = store.GetSetting("FluentAssertions.TestFramework");
testFrameworkName = store.GetSetting(TestFrameworkConfigurationKey);
}

return testFrameworkName;
Expand Down
34 changes: 19 additions & 15 deletions Src/FluentAssertions/Execution/TestFrameworkProvider.cs
Expand Up @@ -6,6 +6,9 @@

namespace FluentAssertions.Execution;

/// <summary>
/// Implements a wrapper around all supported test frameworks to throw the correct assertion exception.
/// </summary>
internal static class TestFrameworkProvider
{
#region Private Definitions
Expand All @@ -28,24 +31,24 @@ public static void Throw(string message)
{
if (testFramework is null)
{
testFramework = DetectFramework();
testFramework = DetectFramework(Services.Configuration);
}

testFramework.Throw(message);
}

private static ITestFramework DetectFramework()
internal static ITestFramework DetectFramework(Configuration configuration)
{
ITestFramework detectedFramework = AttemptToDetectUsingAppSetting()
ITestFramework detectedFramework = AttemptToDetectUsingAppSetting(configuration)
?? AttemptToDetectUsingDynamicScanning()
?? new FallbackTestFramework();

return detectedFramework;
}

private static ITestFramework AttemptToDetectUsingAppSetting()
internal static ITestFramework AttemptToDetectUsingAppSetting(Configuration configuration)
{
string frameworkName = Services.Configuration.TestFrameworkName;
string frameworkName = configuration.TestFrameworkName;
if (string.IsNullOrEmpty(frameworkName))
{
return null;
Expand All @@ -54,22 +57,23 @@ private static ITestFramework AttemptToDetectUsingAppSetting()
if (!Frameworks.TryGetValue(frameworkName, out ITestFramework framework))
{
string frameworks = string.Join(", ", Frameworks.Keys);
var message = $"FluentAssertions was configured to use {frameworkName} but the requested test framework is not supported. " +
$"Please use one of the supported frameworks: {frameworks}";
var message = $"FluentAssertions was configured to use the test framework '{frameworkName}' but this is not supported. " +
$"Please use one of the supported frameworks: {frameworks}.";

throw new Exception(message);
throw new InvalidOperationException(message);
}

if (!framework.IsAvailable)
{
string frameworks = string.Join(", ", Frameworks.Keys);
var message = framework is LateBoundTestFramework lateBoundTestFramework
? $"FluentAssertions was configured to use {frameworkName} but the required test framework assembly {lateBoundTestFramework.AssemblyName} could not be found. " +
$"Please use one of the supported frameworks: {frameworks}"
: $"FluentAssertions was configured to use {frameworkName} but the required test framework could not be found. " +
$"Please use one of the supported frameworks: {frameworks}";

throw new Exception(message);
var innerMessage = framework is LateBoundTestFramework lateBoundTestFramework
? $"the required assembly '{lateBoundTestFramework.AssemblyName}' could not be found"
: "it could not be found";
var message =
$"FluentAssertions was configured to use the test framework '{frameworkName}' but {innerMessage}. " +
$"Please use one of the supported frameworks: {frameworks}.";

throw new InvalidOperationException(message);
}

return framework;
Expand Down
@@ -0,0 +1,81 @@
using System;
using FluentAssertions.Common;
using FluentAssertions.Execution;
using Xunit;

using static FluentAssertions.FluentActions;

namespace FluentAssertions.Specs.Execution;

public class TestFrameworkProviderTests
{
[Fact]
public void When_running_xunit_test_implicitly_it_should_be_detected()
{
var configuration = new Configuration(new TestConfigurationStore());
var result = TestFrameworkProvider.DetectFramework(configuration);

result.IsAvailable.Should().BeTrue();
result.Should().BeOfType<XUnit2TestFramework>();
}

[Fact]
public void When_running_xunit_test_explicitly_it_should_be_detected()
{
var configuration = new Configuration(new TestConfigurationStore())
{
TestFrameworkName = "xunit2"
};
var result = TestFrameworkProvider.DetectFramework(configuration);

result.IsAvailable.Should().BeTrue();
result.Should().BeOfType<XUnit2TestFramework>();
}

[Fact]
public void When_running_test_with_unknown_test_framework_it_should_throw()
{
var configuration = new Configuration(new TestConfigurationStore())
{
TestFrameworkName = "foo"
};

Invoking(() => TestFrameworkProvider.AttemptToDetectUsingAppSetting(configuration))
.Should().Throw<InvalidOperationException>()
.WithMessage("FluentAssertions was configured to use the test framework 'foo' but this is not supported."
+ " Please use one of the supported frameworks: mspec, nspec3, nunit, mstestv2, xunit2.");
}

[Fact]
public void When_running_test_with_direct_bound_but_unavailable_test_framework_it_should_throw()
{
var configuration = new Configuration(new TestConfigurationStore())
{
TestFrameworkName = "nspec3"
};

Invoking(() => TestFrameworkProvider.AttemptToDetectUsingAppSetting(configuration))
.Should().Throw<InvalidOperationException>()
.WithMessage("FluentAssertions was configured to use the test framework 'nspec3' but it could not be found."
+ " Please use one of the supported frameworks: mspec, nspec3, nunit, mstestv2, xunit2.");
}

[Fact]
public void When_running_test_with_late_bound_but_unavailable_test_framework_it_should_throw()
{
var configuration = new Configuration(new TestConfigurationStore())
{
TestFrameworkName = "nunit"
};

Invoking(() => TestFrameworkProvider.AttemptToDetectUsingAppSetting(configuration))
.Should().Throw<InvalidOperationException>()
.WithMessage("FluentAssertions was configured to use the test framework 'nunit' but the required assembly 'nunit.framework' could not be found."
+ " Please use one of the supported frameworks: mspec, nspec3, nunit, mstestv2, xunit2.");
}

private sealed class TestConfigurationStore : IConfigurationStore
{
string IConfigurationStore.GetSetting(string name) => string.Empty;
}
}

0 comments on commit f9a958a

Please sign in to comment.