diff --git a/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs b/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs index aaecccf79..bd0399b5e 100644 --- a/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs +++ b/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs @@ -79,7 +79,17 @@ public class ActivityTestFixture public async Task ExecuteAsync() { var context = await BuildAsync(); - + return await ExecuteAsync(context); + } + + /// + /// Executes the activity using a pre-built . + /// Useful when you need to customize the context before execution, such as setting initial workflow state or overriding correlation IDs. + /// + /// The pre-built context to execute + /// The after execution + public async Task ExecuteAsync(ActivityExecutionContext context) + { // Set up variables and inputs, then execute the activity await SetupExistingVariablesAsync(Activity, context); await context.EvaluateInputPropertiesAsync(); diff --git a/test/unit/Elsa.Activities.UnitTests/Primitives/CorrelateTests.cs b/test/unit/Elsa.Activities.UnitTests/Primitives/CorrelateTests.cs new file mode 100644 index 000000000..fc7586cdf --- /dev/null +++ b/test/unit/Elsa.Activities.UnitTests/Primitives/CorrelateTests.cs @@ -0,0 +1,54 @@ +using Elsa.Testing.Shared; + +namespace Elsa.Activities.UnitTests.Primitives; + +public class CorrelateTests +{ + [Fact] + public async Task Should_Set_CorrelationId_From_String_Literal() + { + const string expected = "test-correlation-id"; + var correlate = new Correlate(expected, null, null); + + await AssertCorrelationIdAsync(correlate, expected); + } + + [Fact] + public async Task Should_Set_CorrelationId_From_Input() + { + const string expected = "dynamic-correlation-id"; + var correlate = new Correlate { CorrelationId = new(expected) }; + + await AssertCorrelationIdAsync(correlate, expected); + } + + [Fact] + public async Task Should_Set_CorrelationId_From_Func() + { + const string expected = "func-correlation-id"; + var correlate = new Correlate(_ => expected); + + await AssertCorrelationIdAsync(correlate, expected); + } + + [Fact] + public async Task Should_Overwrite_Existing_CorrelationId() + { + const string initial = "initial-correlation-id"; + const string expected = "updated-correlation-id"; + var correlate = new Correlate(expected, null, null); + var fixture = new ActivityTestFixture(correlate); + + var context = await fixture.BuildAsync(); + context.WorkflowExecutionContext.CorrelationId = initial; + await fixture.ExecuteAsync(context); + + Assert.Equal(expected, context.WorkflowExecutionContext.CorrelationId); + } + + private static async Task AssertCorrelationIdAsync(Correlate correlate, string expected) + { + var context = await new ActivityTestFixture(correlate).ExecuteAsync(); + Assert.Equal(expected, context.WorkflowExecutionContext.CorrelationId); + } +}