Add test for activities and variables

This commit is contained in:
Sipke Schoorstra 2023-10-20 13:42:18 +02:00
parent bd1eb2edfa
commit abff7f3907
6 changed files with 91 additions and 2 deletions

View file

@ -130,6 +130,7 @@
</ItemGroup>

View file

@ -12,12 +12,11 @@ public class Tests
{
private readonly IWorkflowRunner _workflowRunner;
private readonly CapturingTextWriter _capturingTextWriter = new();
private readonly IWorkflowBuilderFactory _workflowBuilderFactory;
public Tests(ITestOutputHelper testOutputHelper)
{
var services = new TestApplicationBuilder(testOutputHelper).WithCapturingTextWriter(_capturingTextWriter).Build();
_workflowBuilderFactory = services.GetRequiredService<IWorkflowBuilderFactory>();
services.GetRequiredService<IWorkflowBuilderFactory>();
_workflowRunner = services.GetRequiredService<IWorkflowRunner>();
}

View file

@ -0,0 +1,12 @@
using Elsa.Extensions;
using Elsa.Workflows.Core;
namespace Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities.Activities;
public class CreateVariableActivity : CodeActivity
{
protected override void Execute(ActivityExecutionContext context)
{
context.SetVariable("Foo", "Bar");
}
}

View file

@ -0,0 +1,13 @@
using Elsa.Extensions;
using Elsa.Workflows.Core;
namespace Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities.Activities;
public class ReadVariableActivity : CodeActivity<string>
{
protected override void Execute(ActivityExecutionContext context)
{
var foo = context.GetVariable<string>("Foo");
Result.Set(context, foo);
}
}

View file

@ -0,0 +1,31 @@
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows.Core.Contracts;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;
namespace Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities;
public class Tests
{
private readonly IWorkflowRunner _workflowRunner;
private readonly CapturingTextWriter _capturingTextWriter = new();
public Tests(ITestOutputHelper testOutputHelper)
{
var services = new TestApplicationBuilder(testOutputHelper).WithCapturingTextWriter(_capturingTextWriter).Build();
services.GetRequiredService<IWorkflowBuilderFactory>();
_workflowRunner = services.GetRequiredService<IWorkflowRunner>();
}
[Fact(DisplayName = "Activities can set and get variables internally")]
public async Task Test1()
{
await _workflowRunner.RunAsync<SampleWorkflow>();
var lines = _capturingTextWriter.Lines.ToList();
Assert.Equal(new[] { "Bar" }, lines);
}
}

View file

@ -0,0 +1,33 @@
using Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities.Activities;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Memory;
namespace Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities.Workflows;
class SampleWorkflow : WorkflowBase
{
protected override void Build(IWorkflowBuilder workflow)
{
var variable1 = new Variable<string>();
workflow.Root = new Sequence
{
Variables =
{
variable1
},
Activities =
{
new CreateVariableActivity(),
new ReadVariableActivity
{
Result = new(variable1)
},
new WriteLine(variable1)
}
};
}
}