DrifterApps.Seeds.FluentScenario 0.1.4

dotnet add package DrifterApps.Seeds.FluentScenario --version 0.1.4                
NuGet\Install-Package DrifterApps.Seeds.FluentScenario -Version 0.1.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DrifterApps.Seeds.FluentScenario" Version="0.1.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DrifterApps.Seeds.FluentScenario --version 0.1.4                
#r "nuget: DrifterApps.Seeds.FluentScenario, 0.1.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DrifterApps.Seeds.FluentScenario as a Cake Addin
#addin nuget:?package=DrifterApps.Seeds.FluentScenario&version=0.1.4

// Install DrifterApps.Seeds.FluentScenario as a Cake Tool
#tool nuget:?package=DrifterApps.Seeds.FluentScenario&version=0.1.4                

Fluent Scenario

FluentScenario is a C# library that provides a fluent BDD scenario testing framework.

Table of Contents

Installation

To install FluentScenario, you can use the NuGet package manager:

dotnet add package DrifterApps.Seeds.FluentScenario

If you use FluentAssertions to assert your tests. You can use the NuGet package manager:

dotnet add package DrifterApps.Seeds.FluentScenario.FluentAssertions

Usage

Scenario basics

Here is a basic test scenario ScenarioBasicsTests:

[Fact]
public async Task SimpleScenario()
{
    int temperatureInside = 0;
    int temperatureOutside = 0;

    await ScenarioRunner.Create("when the weather is too cold", _scenarioOutput)
        .Given("I want to go play outside", () => temperatureInside = 19)
        .When("the temperature is below 0C", () => temperatureOutside = -5)
        .Then("I stay inside if the temperature difference is greater than 20C", () => (temperatureInside - temperatureOutside).Should().BeGreaterThan(20))
        .PlayAsync();
}

This will produce this output:

✓ SCENARIO for when the weather is too cold
✓ GIVEN I want to go play outside
✓ WHEN the temperature is below 0C
✓ THEN I stay inside if the temperature difference is greater than 20C

Scenario with execution methods

For scenarios where some steps could be shared between multiple scenarions, you can create a set of execution methods for those common steps ScenarioWithExecutionMethodsTests:

[Fact]
public async Task ScenarioWithExecutionMethods()
{
    await ScenarioRunner.Create("when the weather is too cold", _scenarioOutput)
        .Given(PlayOutsideMethod)
        .When(TemperatureBelow0CMethod)
        .Then(StayInsideMethod)
        .PlayAsync();
}

private static void PlayOutsideMethod(IStepRunner runner)
{
    runner.Execute("I want to go play outside", () =>
    {
        runner.SetContextData("temperatureInside", 19);
    });
}

private static void TemperatureBelow0CMethod(IStepRunner runner)
{
    runner.Execute("the temperature is below 0C", () =>
    {
        runner.SetContextData("temperatureOutside", -5);
    });
}

private static void StayInsideMethod(IStepRunner runner)
{
    runner.Execute("I stay inside if the temperature difference is greater than 20C", () =>
    {
        var temperatureInside = runner.GetContextData<int>("temperatureInside");
        var temperatureOutside = runner.GetContextData<int>("temperatureOutside");
        (temperatureInside - temperatureOutside).Should().BeGreaterThan(20);
    });
}

This will produce this output:

✓ SCENARIO for when the weather is too cold
✓ GIVEN I want to go play outside
✓ WHEN the temperature is below 0C
✓ THEN I stay inside if the temperature difference is greater than 20C

Scenario with well named methods

Some of us like to properly name our methods so they are clear to the reader what the intent of the method is.

In this case, you can forgo a description and the ScenarioRunner will use the name of your calling methods to create the description ScenarioWithWellNamedMethodsTests:

[Fact]
public async Task WhenTheWeatherIsTooCold()
{
    await ScenarioRunner.Create(_scenarioOutput)
        .Given(IWantToGoPlayOutside)
        .When(TheTemperatureIsBelow0C)
        .Then(IStayInsideIfTheTemperatureDifferenceIsGreaterThan20C)
        .PlayAsync();
}

private static void IWantToGoPlayOutside(IStepRunner runner)
{
    runner.Execute(() =>
    {
        runner.SetContextData("temperatureInside", 19);
    });
}

private static void TheTemperatureIsBelow0C(IStepRunner runner)
{
    runner.Execute(() =>
    {
        runner.SetContextData("temperatureOutside", -5);
    });
}

private static void IStayInsideIfTheTemperatureDifferenceIsGreaterThan20C(IStepRunner runner)
{
    runner.Execute(() =>
    {
        var temperatureInside = runner.GetContextData<int>("temperatureInside");
        var temperatureOutside = runner.GetContextData<int>("temperatureOutside");
        (temperatureInside - temperatureOutside).Should().BeGreaterThan(20);
    });
}

This will produce this output:

✓ SCENARIO for When The Weather Is Too Cold
✓ GIVEN I Want To Go Play Outside
✓ WHEN The Temperature Is Below0 C
✓ THEN I Stay Inside If The Temperature Difference Is Greater Than20 C

Scenario with parameters and results

To start a scenario, you can pass values in the Create method. And you can also pass values between steps.

Each values passed as a parameter is a Enzure<> value object. This value object is there to help ensuring that the value received is a valid value.

Here's an example ScenarioWithParametersAndResultsTests:

[Theory]
[InlineData(19)]
public async Task ScenarioWithParameters(int inside)
{
    await ScenarioRunner.Create("when the weather is too cold", inside, _scenarioOutput)
        .Given("I want to go play outside", (Ensure<int> temperatureInside) =>
        {
            temperatureInside.Should().BeValid(); // ensure the temperature is valid
            return (temperatureInside.Value, -5);
        })
        .When("the temperature difference is great", (Ensure<(int inside, int outside)> temperatures) =>
        {
            temperatures.Should().BeValid(); // ensure the temperatures are valid
            return temperatures.Value.inside - temperatures.Value.outside;
        })
        .Then("I stay inside if the temperature difference is greater than 20C",
            (Ensure<int> temperatureDifference) =>
            {
                temperatureDifference.Should().BeValid().And.Subject.Value.Should().BeGreaterThan(20);
            })
        .PlayAsync();
}

This will produce this output:

✓ SCENARIO for when the weather is too cold
✓ GIVEN I want to go play outside
✓ WHEN the temperature difference is great
✓ THEN I stay inside if the temperature difference is greater than 20C

Contributing

Contributions are welcome! Please read the contributing guidelines for more information.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DrifterApps.Seeds.FluentScenario:

Package Downloads
DrifterApps.Seeds.Testing

Package Description

DrifterApps.Seeds.FluentScenario.FluentAssertions

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.4 275 12/3/2024
0.1.2 255 11/26/2024
0.1.0 95 11/26/2024