elsa-workflows / elsa-workflows/elsa-core

Setting the result and outcomes of custom activity that was started in unit test, sets the result to null instead of the correct value and the outocmes array is empty

Open
#5,453 1 comment 0 reactions 0 assignees View on GitHub
bug triaged
Dominant language
C#
Stars
7.9k
Forks
1.5k
Avg merge
15h 22m
Merged PRs (30d)
114

Description

## Description
Using ActivityExecutionContext.Set() to set the result of my Activity started from an unit test, leaves the result as null. Also the activity outcomes is not set as well.

## Steps to Reproduce
1. Make a custom activity similar to the example provided below

```
[FlowNode(Outcomes.Success, Outcomes.Failed)]
public class MyActivity() : Activity
{
//input fields
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
// code
context.CreateBookmark(new CreateBookmark
{
Payload = new WaitForActivityBookmark
{
Identifier = $"{context.Id}-{context.WorkflowExecutionContext.Id}"
},
Callback = ResumeAsync
});
}

private async ValueTask ResumeAsync(ActivityExecutionContext context)
{
Value = //code to get the value, this part was mocked and works fine

context.Set(Result,Value); //trying to set the result of my activity

if(Value.isSuccess)
{
await context.CompleteActivityWithOutcomesAsync("Success"); // trying to set outcome of the activity
}
else
{
await context.CompleteActivityWithOutcomesAsync("Failed"); // trying to set outcome of the activity
}
}
}

```
2. Make a unit test for this activity using Xunit. My unit test looks like this:

```
public MyActivityTest(ITestOutputHelper testOutputHelper)
{
_serviceProvider = new TestApplicationBuilder(
testOutputHelper)
.ConfigureServices(
serv =>
{
//other services...
serv.AddElsa(elsa =>
{
elsa.AddActivity();
elsa.UseDefaultAuthentication();

elsa.UseWorkflowsApi();

elsa.UseRealTimeWorkflows();

elsa.UseCSharp();
elsa.UseJavaScript();
elsa.UseLiquid();

elsa.UseHttp();
elsa.UseScheduling();

elsa.AddSwagger();
});
serv.AddElsaCore();
})
.Build();
}

[Fact(DisplayName = "MyActivity_Succeeds")]
public async Task MyActivity_Succeeds()
{
//making some input for my activity

var activity = new MyActivity()
{
//setting the required input
}

var workflowRunner = _serviceProvider.GetRequiredService();
var receivedResponse = await _serviceProvider.RunActivityAsync(activity); //starting the activity
var responseMock = new MyActivityResponse()
{
WorkflowIdentifier =
$"{receivedResponse.WorkflowState.ActivityExecutionContexts.FirstOrDefault()?.Id}-{receivedResponse.WorkflowState.Id}",
IsSuccess = true,
//other fields
}; // this will be received by the Value from ResumeAsync

var workflow = receivedResponse.Workflow;
var workflowState = receivedResponse.WorkflowState;
var bookmark = receivedResponse.WorkflowState.Bookmarks.Single();

var options = new RunWorkflowOptions()
{
BookmarkId = bookmark.Id,
ParentWorkflowInstanceId = workflowState.ParentWorkflowInstanceId,
ActivityId = bookmark.ActivityId,
ActivityInstanceId = bookmark.ActivityInstanceId,
ActivityNodeId = bookmark.ActivityNodeId,
};

// resume the activity based on the bookmark
receivedResponse = await workflowRunner.RunAsync(
workflow,
workflowState,
options,
cancellationToken: CancellationToken.None);
}
```

## Expected Behavior
The expected behavior would be to have the Value set as Result for my activity and for the Outcomes to not be empty.

## Environment
- **Elsa Package Version**: Elsa 3.1.2
- **Operating System**: Windows11

## Troubleshooting Attempts
Attempted to mock the Set() method but It gave the following error: System.NotSupportedException: 'Unsupported expression: x => x.Set(It.IsAny>(), It.IsAny(), "Result")
Non-overridable members (here: ActivityExecutionContext.Set) may not be used in setup / verification expressions.' Similar for SetResult() method.
Tried to use RunUntilEndAsync but got the same results.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.