Skip to content

Commit 2e73362

Browse files
edmondopagautam478
andauthored
Making Workflow and Activity registration optional when they are mocked (#1256)
Co-authored-by: agautam478 <[email protected]>
1 parent e31ba38 commit 2e73362

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

internal/workflow_testsuite.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ func (t *TestWorkflowEnvironment) OnActivity(activity interface{}, args ...inter
240240
panic(err)
241241
}
242242
fnName := getActivityFunctionName(t.impl.registry, activity)
243+
t.impl.registry.RegisterActivityWithOptions(activity, RegisterActivityOptions{DisableAlreadyRegisteredCheck: true})
243244
call = t.Mock.On(fnName, args...)
244-
245245
case reflect.String:
246246
call = t.Mock.On(activity.(string), args...)
247247
default:
@@ -289,11 +289,12 @@ func (t *TestWorkflowEnvironment) OnWorkflow(workflow interface{}, args ...inter
289289
if alias, ok := t.impl.registry.getWorkflowAlias(fnName); ok {
290290
fnName = alias
291291
}
292+
t.impl.registry.RegisterWorkflowWithOptions(workflow, RegisterWorkflowOptions{DisableAlreadyRegisteredCheck: true})
292293
call = t.Mock.On(fnName, args...)
293294
case reflect.String:
294295
call = t.Mock.On(workflow.(string), args...)
295296
default:
296-
panic("activity must be function or string")
297+
panic("workflow must be function or string")
297298
}
298299

299300
return t.wrapCall(call)

internal/workflow_testsuite_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package internal
2323

2424
import (
2525
"context"
26+
"github.com/stretchr/testify/mock"
2627
"github.com/stretchr/testify/suite"
2728
"strings"
2829
"testing"
@@ -129,6 +130,53 @@ func TestWorkflowReturnNil(t *testing.T) {
129130
require.NoError(t, err)
130131
}
131132

133+
func HelloWorkflow(ctx Context, name string) (string, error) {
134+
ctx = WithActivityOptions(ctx, ActivityOptions{
135+
ScheduleToCloseTimeout: time.Hour,
136+
StartToCloseTimeout: time.Hour,
137+
ScheduleToStartTimeout: time.Hour,
138+
})
139+
var result string
140+
err := ExecuteActivity(ctx, HelloActivity, name).Get(ctx, &result)
141+
return result, err
142+
}
143+
144+
func HelloActivity(ctx context.Context, name string) (string, error) {
145+
return "Hello " + name + "!", nil
146+
}
147+
148+
func TestWorkflowMockingWithoutRegistration(t *testing.T) {
149+
testSuite := &WorkflowTestSuite{}
150+
env := testSuite.NewTestWorkflowEnvironment()
151+
env.OnWorkflow(HelloWorkflow, mock.Anything, mock.Anything).Return(
152+
func(ctx Context, person string) (string, error) {
153+
return "Hello " + person + "!", nil
154+
})
155+
// Workflow is mocked, no activity registration required
156+
env.ExecuteWorkflow(HelloWorkflow, "Cadence")
157+
require.NoError(t, env.GetWorkflowError())
158+
var result string
159+
err := env.GetWorkflowResult(&result)
160+
require.NoError(t, err)
161+
require.Equal(t, "Hello Cadence!", result)
162+
}
163+
164+
func TestActivityMockingWithoutRegistration(t *testing.T) {
165+
testSuite := &WorkflowTestSuite{}
166+
env := testSuite.NewTestWorkflowEnvironment()
167+
env.OnActivity(HelloActivity, mock.Anything, mock.Anything).Return(
168+
func(ctx context.Context, person string) (string, error) {
169+
return "Goodbye " + person + "!", nil
170+
})
171+
// Registration of activity not required
172+
env.RegisterWorkflow(HelloWorkflow)
173+
env.ExecuteWorkflow(HelloWorkflow, "Cadence")
174+
require.NoError(t, env.GetWorkflowError())
175+
var result string
176+
err := env.GetWorkflowResult(&result)
177+
require.NoError(t, err)
178+
require.Equal(t, "Goodbye Cadence!", result)
179+
132180
type InterceptorTestSuite struct {
133181
suite.Suite
134182
WorkflowTestSuite

0 commit comments

Comments
 (0)