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

Add mock call assertions to TestWorkflowEnvironment #748

Merged
merged 3 commits into from
Mar 10, 2022

Conversation

sebneira
Copy link
Contributor

@sebneira sebneira commented Mar 8, 2022

What was changed

Added AssertCalled, AssertNotCalled and AssertNumberOfCalls to TestWorkflowEnvironment to enable a straightforward way to assert for activity calls within workflows.

They have been implemented following the same approach as https://pkg.go.dev/github.com/stretchr/testify/mock#Mock.

Why?

Asserting how an activity has been called from within a workflow (e.g. storing/retrieve data from a db) can be cumbersome without assertions on how mocks have been called.

The current best approach are:

  • storing the arguments in variables to later assert them
  • asserting within the mocks handler function (which leads to very long stacks when the assertion fails)

The testify library provides AssertCalled, AssertNotCalled and AssertNumberOfCalls for this purpose, making the assertions straight-forward to understand and debug.

Checklist

  1. How was this tested:
    Two unit tests have been implemented to test these cases.

  2. Any docs updates needed?
    In https://docs.temporal.io/docs/go/how-to-test-workflow-definitions-in-go/, Test_SimpleWorkflow_ActivityParamCorrect could be updated to read

func (s *UnitTestSuite) Test_SimpleWorkflow_ActivityParamCorrect() {
        s.env.OnActivity(SimpleActivity, mock.Anything, mock.Anything).Return("", nil)
        s.env.ExecuteWorkflow(SimpleWorkflow, "test_success")

        s.env.AssertCalled(s.T(), SimpleActivity, mock.Anything, "test_success")
        s.env.AssertNumberOfCalls(s.T(), SimpleActivity, 1)
        s.True(s.env.IsWorkflowCompleted())
        s.NoError(s.env.GetWorkflowError())
}

@CLAassistant
Copy link

CLAassistant commented Mar 8, 2022

CLA assistant check
All committers have signed the CLA.

@sebneira sebneira marked this pull request as draft March 8, 2022 16:01
@cretz
Copy link
Member

cretz commented Mar 8, 2022

@sikian - This looks good to me, thanks! Mark it as ready for review instead of draft and I'll gladly approve and merge it.

@sebneira sebneira marked this pull request as ready for review March 8, 2022 16:11
@sebneira
Copy link
Contributor Author

sebneira commented Mar 8, 2022

Thanks @cretz!
Wanted to have the team check this before you guys got to it but happy to see you're ok merging as is.

@cretz
Copy link
Member

cretz commented Mar 8, 2022

Thanks! I approved. I'll leave it open for a bit to see if anyone else wants to look.

@cretz cretz merged commit 2a582f8 into temporalio:master Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants