elsa-core/test/unit/Elsa.Activities.UnitTests/Branching/FlowSwitchTests.cs
Sipke Schoorstra d0a4520e7d
Adds unit tests for FlowSwitch activity (#6996)
* Add unit tests for `FlowFork` activity and enhance test utilities

- Introduce `FlowForkTests` to validate activity behavior with various branch configurations.
- Extend `ActivityExecutionContextExtensions` with methods to retrieve and check outcomes.
- Modify `ActivityTestFixture` to ensure activities transition to `Running` status before execution.

* Add unit tests for `FlowSwitch` activity and extend test utilities

- Implement `FlowSwitchTests` to verify behavior based on switch cases, modes, and literal expressions.
- Enhance `ActivityExecutionContextExtensions` with methods to retrieve and check outcomes in execution context.

* Update test guidelines to include examples for checking activity outcomes

- Add unit test examples for validating multiple and default outcomes.
- Extend documentation to describe new `ActivityExecutionContextExtensions` methods: `GetOutcomes` and `HasOutcome`.

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Simplify `GetOutcomes` method in `ActivityExecutionContextExtensions`.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-21 15:36:12 +02:00

100 lines
2.9 KiB
C#

using Elsa.Expressions.Models;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Elsa.Workflows.Activities.Flowchart.Activities;
using Elsa.Workflows.Activities.Flowchart.Models;
namespace Elsa.Activities.UnitTests.Branching;
public class FlowSwitchTests
{
[Theory]
[MemberData(nameof(DefaultOutcomeTestCases))]
public async Task Should_Return_Default_When_No_Cases_Match(List<FlowSwitchCase> cases)
{
// Arrange
var flowSwitch = new FlowSwitch { Cases = cases };
// Act
var context = await ExecuteAsync(flowSwitch);
// Assert - Activity should return Default outcome when no cases match.
Assert.True(context.HasOutcome("Default"));
}
[Theory]
[MemberData(nameof(SwitchModeTestCases))]
public async Task Should_Return_Outcomes_Based_On_Switch_Mode(
List<FlowSwitchCase> cases,
SwitchMode mode,
string[] expectedOutcomes)
{
// Arrange
var flowSwitch = new FlowSwitch
{
Cases = cases,
Mode = new(mode)
};
// Act
var context = await ExecuteAsync(flowSwitch);
// Assert - Activity should return outcomes based on switch mode.
var outcomes = context.GetOutcomes().ToList();
Assert.Equal(expectedOutcomes.Length, outcomes.Count);
foreach (var expectedOutcome in expectedOutcomes)
Assert.Contains(expectedOutcome, outcomes);
}
[Fact]
public async Task Should_Evaluate_Cases_With_Literal_Expression()
{
// Arrange
var flowSwitch = new FlowSwitch
{
Cases = new List<FlowSwitchCase>
{
new("TrueCase", Expression.LiteralExpression(true)),
new("FalseCase", Expression.LiteralExpression(false))
}
};
// Act
var context = await ExecuteAsync(flowSwitch);
// Assert - Activity should match the case with true literal.
Assert.True(context.HasOutcome("TrueCase"));
}
public static IEnumerable<object[]> DefaultOutcomeTestCases()
{
yield return [new List<FlowSwitchCase>()];
yield return
[
new List<FlowSwitchCase>
{
new("Case1", () => false),
new("Case2", () => false)
}
];
}
public static IEnumerable<object[]> SwitchModeTestCases()
{
var cases = new List<FlowSwitchCase>
{
new("Case1", () => false),
new("Case2", () => true),
new("Case3", () => true)
};
yield return [cases, SwitchMode.MatchFirst, new[] { "Case2" }];
yield return [cases, SwitchMode.MatchAny, new[] { "Case2", "Case3" }];
}
private static Task<ActivityExecutionContext> ExecuteAsync(IActivity activity)
{
return new ActivityTestFixture(activity).ExecuteAsync();
}
}