< Summary

Information
Class: Elsa.Testing.Shared.ActivityTestFixture
Assembly: Elsa.Testing.Shared
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 72
Coverable lines: 72
Total lines: 186
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_Activity()100%210%
get_Services()100%210%
ConfigureServices(...)100%210%
ConfigureContext(...)100%210%
ExecuteAsync()100%210%
ExecuteAsync()100%210%
BuildAsync()0%620%
SetupExistingVariablesAsync(...)0%4260%
AddCoreWorkflowServices(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Common.Multitenancy;
 3using Elsa.Expressions.Contracts;
 4using Elsa.Expressions.Services;
 5using Elsa.Extensions;
 6using Elsa.Mediator.Contracts;
 7using Elsa.Workflows;
 8using Elsa.Workflows.Activities;
 9using Elsa.Workflows.CommitStates;
 10using Elsa.Workflows.Management.Providers;
 11using Elsa.Workflows.Management.Services;
 12using Elsa.Workflows.Memory;
 13using Elsa.Workflows.PortResolvers;
 14using JetBrains.Annotations;
 15using Microsoft.Extensions.DependencyInjection;
 16using NSubstitute;
 17
 18namespace Elsa.Testing.Shared;
 19
 20/// <summary>
 21/// A test fixture for unit testing activities in isolation.
 22/// Provides a fluent API to configure services, variables, and execution context.
 23/// </summary>
 24public class ActivityTestFixture
 25{
 26    private Action<ActivityExecutionContext>? _configureContextAction;
 27
 28    /// <summary>
 29    /// Initializes a new instance of the <see cref="ActivityTestFixture"/> class.
 30    /// </summary>
 31    /// <param name="activity">The activity to test</param>
 032    public ActivityTestFixture(IActivity activity)
 33    {
 034        Activity = activity;
 035        Services = new ServiceCollection();
 036        AddCoreWorkflowServices(Services);
 037    }
 38
 39    /// <summary>
 40    /// Represents the activity being tested within the context of the activity test fixture.
 41    /// Provides access to the activity for configuration, execution, and validation purposes.
 42    /// </summary>
 043    public IActivity Activity { get; }
 44
 45    /// <summary>
 46    /// Gets the service collection for registering additional services.
 47    /// Use this to add services required by the activity under test.
 48    /// </summary>
 49    [UsedImplicitly]
 050    public IServiceCollection Services { get; private set; }
 51
 52    /// <summary>
 53    /// Configures the service collection using a fluent action.
 54    /// </summary>
 55    /// <param name="configure">Action to configure the service collection</param>
 56    /// <returns>The fixture instance for method chaining</returns>
 57    public ActivityTestFixture ConfigureServices(Action<IServiceCollection> configure)
 58    {
 059        configure(Services);
 060        return this;
 61    }
 62
 63    /// <summary>
 64    /// Configures the activity execution context before execution.
 65    /// Multiple calls to this method will chain the configuration actions together.
 66    /// </summary>
 67    /// <param name="configure">Action to configure the activity execution context</param>
 68    /// <returns>The fixture instance for method chaining</returns>
 69    [UsedImplicitly]
 70    public ActivityTestFixture ConfigureContext(Action<ActivityExecutionContext> configure)
 71    {
 072        _configureContextAction += configure;
 073        return this;
 74    }
 75
 76    /// <summary>
 77    /// Executes the activity and returns the execution context.
 78    /// </summary>
 79    /// <returns>The ActivityExecutionContext after execution</returns>
 80    public async Task<ActivityExecutionContext> ExecuteAsync()
 81    {
 082        var context = await BuildAsync();
 083        return await ExecuteAsync(context);
 084    }
 85
 86    /// <summary>
 87    /// Executes the activity using a pre-built <see cref="ActivityExecutionContext"/>.
 88    /// Useful when you need to customize the context before execution, such as setting initial workflow state or overri
 89    /// </summary>
 90    /// <param name="context">The pre-built context to execute</param>
 91    /// <returns>The <see cref="ActivityExecutionContext"/> after execution</returns>
 92    public async Task<ActivityExecutionContext> ExecuteAsync(ActivityExecutionContext context)
 93    {
 94        // Set up variables and inputs, then execute the activity
 095        await SetupExistingVariablesAsync(Activity, context);
 096        await context.EvaluateInputPropertiesAsync();
 097        context.TransitionTo(ActivityStatus.Running);
 098        await Activity.ExecuteAsync(context);
 99
 0100        return context;
 0101    }
 102
 103    /// <summary>
 104    /// Builds the ActivityExecutionContext without executing the activity.
 105    /// </summary>
 106    public async Task<ActivityExecutionContext> BuildAsync()
 107    {
 0108        var serviceProvider = Services.BuildServiceProvider();
 0109        var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>();
 0110        var workflowGraphBuilder = serviceProvider.GetRequiredService<IWorkflowGraphBuilder>();
 111
 0112        await activityRegistry.RegisterAsync(Activity.GetType());
 113
 0114        var workflow = Workflow.FromActivity(Activity);
 0115        var workflowGraph = await workflowGraphBuilder.BuildAsync(workflow);
 116
 117        // Create workflow execution context using the static factory method
 0118        var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(
 0119            serviceProvider,
 0120            workflowGraph,
 0121            $"test-instance-{Guid.NewGuid()}",
 0122            CancellationToken.None
 0123        );
 124
 125        // Create ActivityExecutionContext for the actual activity we want to test
 0126        var context = await workflowExecutionContext.CreateActivityExecutionContextAsync(Activity);
 127
 128        // Apply any context configuration action
 0129        _configureContextAction?.Invoke(context);
 130
 0131        return context;
 0132    }
 133
 134    /// <summary>
 135    /// Sets up existing variables found on the activity in the execution context.
 136    /// This is necessary because in unit tests, variables need to be initialized.
 137    /// </summary>
 138    private static Task SetupExistingVariablesAsync(IActivity activity, ActivityExecutionContext context)
 139    {
 0140        var activityType = activity.GetType();
 0141        var variableProperties = activityType.GetProperties()
 0142            .Where(p => typeof(Variable).IsAssignableFrom(p.PropertyType))
 0143            .ToList();
 144
 0145        foreach (var variable in variableProperties.Select(property => (Variable?)property.GetValue(activity)))
 146        {
 0147            if(variable == null)
 148                continue;
 149
 0150            context.WorkflowExecutionContext.MemoryRegister.Declare(variable);
 0151            variable.Set(context.ExpressionExecutionContext, variable.Value);
 152        }
 153
 0154        return Task.CompletedTask;
 155    }
 156
 157    private static void AddCoreWorkflowServices(IServiceCollection services)
 158    {
 0159        services.AddLogging();
 0160        services.AddSingleton<ISystemClock>(_ => Substitute.For<ISystemClock>());
 0161        services.AddSingleton<INotificationSender>(_ => Substitute.For<INotificationSender>());
 0162        services.AddSingleton<IActivityVisitor, ActivityVisitor>();
 0163        services.AddScoped<IExpressionEvaluator, ExpressionEvaluator>();
 0164        services.AddSingleton<IWellKnownTypeRegistry, WellKnownTypeRegistry>();
 0165        services.AddSingleton<IActivityDescriber, ActivityDescriber>();
 0166        services.AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>();
 0167        services.AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>();
 0168        services.AddSingleton<IActivityRegistry, ActivityRegistry>();
 0169        services.AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>();
 0170        services.AddScoped<IIdentityGraphService, IdentityGraphService>();
 0171        services.AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>();
 0172        services.AddScoped<IActivityResolver, PropertyBasedActivityResolver>();
 0173        services.AddScoped<IActivityResolver, SwitchActivityResolver>();
 0174        services.AddScoped<DefaultActivityInputEvaluator>();
 0175        services.AddSingleton<IExpressionDescriptorProvider, DefaultExpressionDescriptorProvider>();
 0176        services.AddSingleton<IExpressionDescriptorRegistry, ExpressionDescriptorRegistry>();
 0177        services.AddSingleton<IIdentityGenerator>(_ => Substitute.For<IIdentityGenerator>());
 0178        services.AddSingleton<IHasher>(_ => Substitute.For<IHasher>());
 0179        services.AddSingleton<IStimulusHasher, StimulusHasher>();
 0180        services.AddSingleton<ICommitStateHandler>(_ => Substitute.For<ICommitStateHandler>());
 0181        services.AddSingleton<IActivitySchedulerFactory, ActivitySchedulerFactory>();
 0182        services.AddSingleton<IWorkflowExecutionContextSchedulerStrategy, FakeWorkflowExecutionContextSchedulerStrategy>
 0183        services.AddSingleton<IActivityExecutionContextSchedulerStrategy, FakeActivityExecutionContextSchedulerStrategy>
 0184        services.AddSingleton<ITenantAccessor, DefaultTenantAccessor>();
 0185    }
 186}