Add unit tests for Correlate activity and enhance ActivityTestFixture execution functionality (#7119)

* Add unit tests for `Correlate` activity and enhance `ActivityTestFixture` execution functionality

- Introduced `CorrelateTests` to validate correlation ID handling with string literals, inputs, and functions.
- Extended `ActivityTestFixture` with new `ExecuteAsync(ActivityExecutionContext)` overload for improved test execution customization.

* Update src/common/Elsa.Testing.Shared/ActivityTestFixture.cs

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:
Sipke Schoorstra 2025-11-27 20:57:48 +01:00 committed by GitHub
parent e261a91127
commit e3a8e533c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 1 deletions

View file

@ -79,7 +79,17 @@ public class ActivityTestFixture
public async Task<ActivityExecutionContext> ExecuteAsync()
{
var context = await BuildAsync();
return await ExecuteAsync(context);
}
/// <summary>
/// Executes the activity using a pre-built <see cref="ActivityExecutionContext"/>.
/// Useful when you need to customize the context before execution, such as setting initial workflow state or overriding correlation IDs.
/// </summary>
/// <param name="context">The pre-built context to execute</param>
/// <returns>The <see cref="ActivityExecutionContext"/> after execution</returns>
public async Task<ActivityExecutionContext> ExecuteAsync(ActivityExecutionContext context)
{
// Set up variables and inputs, then execute the activity
await SetupExistingVariablesAsync(Activity, context);
await context.EvaluateInputPropertiesAsync();

View file

@ -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);
}
}