Add unit tests for FlowFork activity and enhance test utilities (#6995)
* 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. * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
af84a70ddb
commit
f7b7ea4d73
|
|
@ -17,4 +17,16 @@ public static class ActivityExecutionContextExtensions
|
|||
{
|
||||
return activityExecutionContext.WorkflowExecutionContext.Scheduler.Find(x => x.Activity == activity) != null;
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetOutcomes(this ActivityExecutionContext activityExecutionContext)
|
||||
{
|
||||
if (activityExecutionContext.JournalData.TryGetValue("Outcomes", out var outcomesObj) && outcomesObj is string[] outcomes)
|
||||
return outcomes;
|
||||
return [];
|
||||
}
|
||||
|
||||
public static bool HasOutcome(this ActivityExecutionContext activityExecutionContext, string outcome)
|
||||
{
|
||||
return activityExecutionContext.GetOutcomes().Contains(outcome);
|
||||
}
|
||||
}
|
||||
|
|
@ -83,6 +83,7 @@ public class ActivityTestFixture
|
|||
// Set up variables and inputs, then execute the activity
|
||||
await SetupExistingVariablesAsync(Activity, context);
|
||||
await context.EvaluateInputPropertiesAsync();
|
||||
context.TransitionTo(ActivityStatus.Running);
|
||||
await Activity.ExecuteAsync(context);
|
||||
|
||||
return context;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
using Elsa.Testing.Shared;
|
||||
using Elsa.Workflows;
|
||||
using Elsa.Workflows.Activities.Flowchart.Activities;
|
||||
|
||||
namespace Elsa.Activities.UnitTests.Branching;
|
||||
|
||||
public class FlowForkTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(BranchTestCases))]
|
||||
public async Task Should_Complete_With_Specified_Branches(string[] branches, string[] expectedOutcomes)
|
||||
{
|
||||
// Arrange
|
||||
var flowFork = new FlowFork();
|
||||
if (branches.Length > 0)
|
||||
flowFork.Branches = new(branches);
|
||||
|
||||
// Act
|
||||
var context = await ExecuteAsync(flowFork);
|
||||
|
||||
// Assert - Activity should complete with all outcomes.
|
||||
var outcomes = context.GetOutcomes().ToList();
|
||||
|
||||
Assert.Equal(expectedOutcomes.Length, outcomes.Count);
|
||||
foreach (var expectedOutcome in expectedOutcomes)
|
||||
Assert.Contains(expectedOutcome, outcomes);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> BranchTestCases()
|
||||
{
|
||||
yield return [Array.Empty<string>(), new[] { "Done" }];
|
||||
yield return [new[] { "SingleBranch" }, new[] { "SingleBranch" }];
|
||||
yield return [new[] { "Branch1", "Branch2", "Branch3" }, new[] { "Branch1", "Branch2", "Branch3" }];
|
||||
}
|
||||
|
||||
private static Task<ActivityExecutionContext> ExecuteAsync(IActivity activity)
|
||||
{
|
||||
return new ActivityTestFixture(activity).ExecuteAsync();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue