Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkflowActivityContext cannot be mocked #1200

Closed
bgelens opened this issue Dec 4, 2023 · 3 comments · Fixed by #1358
Closed

WorkflowActivityContext cannot be mocked #1200

bgelens opened this issue Dec 4, 2023 · 3 comments · Fixed by #1358

Comments

@bgelens
Copy link

bgelens commented Dec 4, 2023

WorkflowActivityContext has an internal constructor only and it's not implementing an interface making it impossible? to mock.

For DaprWorkflowContext I can just use the abstract WorkflowContext so I'm not having this issue when unit testing orchestration workflow.

I suggest the same approach for WorkflowActivityContext as with WorkflowContext, make it abstract without constructor definition and derive a DaprWorkflowActivityContext. Or alternate solutions like adding an interface, make the constructor public or internal protected (that way we can derive a stub I think? by exposing internals via our csproj referencing the workflow package)

I think not being able to mock the ActivityContext is a blocker for us moving forward with Dapr Workflow

@halspang
Copy link
Contributor

halspang commented Dec 5, 2023

/cc @cgillum @RyanLettieri

@TomasEkeli
Copy link

just ran into this trying to drive out the functionality of an activity with tests - this needs to be fixed.

a very ugly, horrible, no-good workaround is to use reflection:

using Xunit;
using NSubstitute;
using FluentAssertions;

// ...

    [Fact] 
    public async Task Call_activity()
    {
        YourAcvitity activity = new(/*dependencies*/);
        // since WorkflowActivityContext only has an internal constructor
        // we need to use reflection to instantiate it
        TaskActivityContext inner = Substitute.For<TaskActivityContext>();
        var context = typeof(WorkflowActivityContext)
            .GetConstructor(
                BindingFlags.NonPublic | BindingFlags.Instance,
                null,
                [typeof(TaskActivityContext)],
                null
            )
            .Invoke([inner]) as WorkflowActivityContext;

        var result = await activity
            .RunAsync(
                context,
                new TInput() // whatever your activity uses
            );

        result.Should().NotBeNull();
    }

@siri-varma
Copy link
Contributor

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants