gotestingtemporal-workflow

temporal error when writing test for workflow that calls child workflow: "unable to find workflow type"


I'm using the golang SDK for temporal. I'm writing a test for my workflow LoadCreateWorkflow. It has one activity and also kicks off a child workflow LoadLifecycleWorkflow.

When writing a unit test for it, I mock out the activity and child workflow LoadLifecycleWorkflow:

    s.env.OnActivity(CreateLoadActivity, mock.Anything, mock.Anything).Return(nil, nil).Once()
    s.env.OnWorkflow(LoadLifecycleWorkflow, mock.Anything).Return(nil)
    s.env.ExecuteWorkflow(LoadCreateWorkflow, wfParams)

(here s.env is the unit test's TestWorkflowEnvironment):

import (
    ...
    "go.temporal.io/sdk/testsuite"
)

type UnitTestSuite struct {
    suite.Suite
    testsuite.WorkflowTestSuite

    env *testsuite.TestWorkflowEnvironment
}

func (s *UnitTestSuite) SetupTest() {
    s.env = s.NewTestWorkflowEnvironment()
}

When running my test I get an error: panic: unable to find workflow type: LoadLifecycleWorkflow. Supported types: [LoadCreateWorkflow]

In my workflow module's init function I register both it and the child workflow:

worker.RegisterWorkflow(LoadCreateWorkflow)
worker.RegisterActivity(CreateLoadActivity)
worker.RegisterWorkflow(LoadLifecycleWorkflow)

Why do I get an error that my LoadLifecycleWorkflow is not registered and how do I fix it?


Solution

  • The child workflow must be registered to the temporal TestWorkflowEnvironment:

    s.env.RegisterWorkflow(LoadLifecycleWorkflow)

    The test workflow environment is a different environment that is just used for writing tests, so any child workflows you expect your workflow to call in your test should be registered to this.